tags:

views:

292

answers:

3

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?

A: 

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

High Performance Mark
that does not work because sorting is done in only one of the three dimensions of color. I can split array further according to first sorted vector and then further sort those and in then end join everything together but that seems too inefficient. I am thinking of converting the three dimensions to strings, concating them, then converting everything back to number in a single dimensional array, sort them and then split the result back into three dimensions. I think that will work. Do you know how I can split a string in matlab into three substrings?
podunk
@podunk, you can index strings the same way you index numerical arrays, with the colon operator.
Doresoom
also, sortrows can sort more than one column at a time. you can specify sortrows(A,[1 3 2]) to sort A first by column 1, then column 3, etc.
Doresoom
A: 

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.

Doresoom
The trouble with using sort as you suggest is that it will break up the colours, for example sort([23 45 56;24 32 12]) will return [23 32 12;24 45 56]. Downvoting.
High Performance Mark
Two extra lines of code more than your solution gets my working answer a downvote?
Doresoom
+5  A: 
%# 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)

alt text

Amro