Finding intersection of two sets in MATLAB is one of the most frequently used function if you are doing lots of data merging tasks. This is achieved by intersect function. However if you would like to do the same for many sets, there are not many alternatives.
Consider that you placed your arrays into a cell vector and would like to get all intersections of a given vector among other remaining arrays and its corresponding indices uniquely. The following function will do the job for you efficiently. It is not O($N^2$) algorithm, so computational cost only grows linearly. I called this function intersectall, code is maintained in the mathworks file exchange; intersectall.
Friday, 26 October 2012
Wednesday, 10 October 2012
Matlab/Octave: imagesc with variable colorbar

% bare minimum - example -
clear all
close all
allLevelNames = {'one', 'two', 'three', 'four'};
levelValues = [1 2 3 4];
myMatrix = [3 3 3 3; 3 3 3 3; 4 4 1 1; 4 4 1 1];
% Now re-write colormap
aV = length(levelValues);
jj = jet(aV);
availableValues = unique(myMatrix);
getIndexes = arrayfun(@(xx) find(levelValues == xx), availableValues);
colormap(jj(getIndexes,:));
% Re-write myMatrix in the availableValues range
myMatrix = arrayfun(@(x) find(availableValues == x), myMatrix);
% Now Plot
h = imagesc(myMatrix);
% Handle if there is more then one color is in use
maxV = max(availableValues);
minV = min(availableValues);
if(length(availableValues) > 1) caxis([minV maxV]); end;
hcb=colorbar;
set(hcb, 'YTick', availableValues);
set(hcb, 'YTickLabel', allLevelNames(getIndexes));
set(gca,'YDir','normal') % Not to invert x-y but putting (0,0) on the bottom left
xlabel('x values');
ylabel('y values (care on (0,0) location from imagesc)');
title({['Variable colormap handling'];['by msuzen at gmail']});
Subscribe to:
Posts (Atom)