The way to do this is to reshape your image into an nx3 matrix where each row correspond to the three colours of a pixel. You can then apply unique with the 'rows' option on this to get your labels. It's then just a matter of reshaping the output.
Note that doing the scanning by rows rather than by columns complicate the code somewhat.
Also, please, post valid matlab code rather than leaving it to us to generate valid matrix
A = cat(3, [22 22 39;89 23 99; 89 69 99], ...
[10 10 40; 11 11 10; 11 10 10], ...
[10 10 50; 14 11 10; 14 10 10])
[~, ~, labels] = unique(reshape(permute(A, [2 1 3]), [], 3), 'rows', 'stable');
B = reshape(labels, size(A, 2), size(A, 1))'
The same code if you do the labeling by column:
[~, ~, labels] = unique(reshape(A, [], 3), 'rows', 'stable');
B = reshape(labels, size(A, 1), size(A, 2))
Best Answer