I have different RGB values in a 260000 * 3 dimension array. I wan to sort these colors in ascending or descending order (it does not matter which) so that similar colors are closer. What's the most efficient way to do this?
At the risk of sounding facetious the intrinsic sortrows()
function is probably adequate -- if not do tell us. But your real difficulties lie in attempting to use RGB triples to define closeness of colour -- is [255,254,255] 'close to' [255,255,0] ?
Regards
Mark
PS Prior to your comment I had assumed that your table of data contained 260000 rows, each row containing 3 numbers which represent the RGB components of your colours. In other words that you had a table of 260000 colours. If this is not correct could you illuminate further
The sort
command would be a good place to start. You should be able to sort by the first dimension, then the second, then the third. I'm not too familiar with RGB values, so I'm not completely sure if sorting the dimensions separately will result in matching colors being grouped together, but some sort of adaptation to the process I described above should do the trick.
EDIT: Including the sort index will avoid breaking up the RGB colors. Example:
x=[5,6;4,7;3,8;2,9;1,10];
[x1,index]=sort(x(:,1));
x2=x(index,2);
x=[x1 x2];
The sortrows command would probably be more elegant, but I was previously not aware of it. HPM's right on that account.
%# sample image
I = repmat(1:100, 100, 1);
%# lets create a normal colormap
C = jet(100);
figure
subplot(121), imagesc(I), colormap(C)
subplot(122), rgbplot(C)
%# shuffle colors
C = C(randperm(100), :);
%# rearrage according to HSV colorspace
C = rgb2hsv(C);
C = sortrows(C, [-1 -3 2]); %# sort first by Hue, then by value
C = hsv2rgb(C);
figure
subplot(121), imagesc(I), colormap(C)
subplot(122), rgbplot(C)