determine within cell the coordinates (rows/columns) having equal 3 numbers

How can you determine within cell 'out' the coordinates (rows/columns) having equal 3 numbers?
I would need to get a matrix that shows me the coordinates with 3 equal values.
rgb = randi(255,3,3,3,'uint8');
out = mat2cell(rgb,ones(1,size(rgb,1)),ones(1,size(rgb,2)),3);

 Accepted Answer

[r,c]=find(cellfun(@(c)all(c==c(1)),out));
If there are none, the above will return [] empty results; taking off the find() will always return a logical array the size of out. What's more convenient will depend mostly on the use.

More Answers (2)

You don't need to convert the data to cell array to find that -
rgb = randi(4,3,3,3,'uint8')
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 4 4 1 3 1 2 3 3 4 rgb(:,:,2) = 4 4 1 3 3 3 1 2 4 rgb(:,:,3) = 2 1 1 4 1 2 1 3 2
%Method 1
[r,c]=find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1))
r = 1
c = 3
%Method 2
[r,c]=find(all(rgb(:,:,2:3)==rgb(:,:,1),3))
r = 1
c = 3
Since the data is an unsinged integer, diff() will not be useful here, otherwise we can use this -
[r,c]=find(all(diff(rgb,1,3)==0,3));
The reason being - Subtracting a bigger number from a smaller number will result in a 0 for any unsigned integer, which will give incorrect result.

11 Comments

"You don't need to convert the data to cell array to find that - "
One presumes the cell array form is the form OP has in the application already and the above machinations are just to provide a sample dataset to illustrate...
You are certainly right in presuming that.
However, the reason I mentioned that is because, in one of their previous questions OP has asked to convert a given 3D numeric array into a cell array, which I happen to have answered and on which OP asked this same question (in continutaion) but deleted their comment afterwards to ask it as a new question.
Exactly. I asked a new question because I was not sure whether to ask the question in the previous discussion.
@Dyuman Joshi Could I ask, using the solution of method 1, how can I select only a range of equal values? For example in the case you showed, select only the range 1:3.
I'm sorry, I didn't understand what you said. Please elaborate on your query.
Suppose that
rgb = randi(4,3,3,3,'uint8')
generates:
rgb = 3×3×3 uint8 array
rgb(:,:,1) =
4 4 1
3 1 2
1 3 4
rgb(:,:,2) =
4 4 1
3 3 2
1 3 4
rgb(:,:,3) =
4 1 1
4 1 2
1 3 2
Considering that the 'rgb' written above:
[r,c] = find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1))
generates:
r = [1; 1; 3; 2; 3];
c = [3; 1; 1; 3; 2];
% number_repeated = [1; 4; 1; 2; 3]; % is just to show which numbers are repeated
I would like to get [r,c] only in the case of repetitions, for example, of the numbers 1 and 2.
Namely, obtain:
r = [1; 3; 3];
c = [3; 1; 2];
% number_repeated = [1; 1; 3]; % is just to show which numbers are repeated
I am a bit confused.
"% number_repeated = [1; 1; 3]; % is just to show which numbers are repeated"
But 3 is not repeated above in - "% number_repeated = [1; 4; 1; 2; 3]; % is just to show which numbers are repeated"
You are right, I was wrong in putting in the correct numbers.
r = [1; 3; 2];
c = [3; 1; 3];
% number_repeated = [1; 1; 2]; % is just to show which numbers are repeated
I am still not sure if I get what you are saying.
Say this is the array we are working with -
rgb(:,:,1) =[4 4 1
3 1 2
1 2 4];
rgb(:,:,2) =[4 4 1
3 3 2
1 2 4];
rgb(:,:,3) =[4 1 1
4 1 2
1 2 2];
rgb = uint8(rgb);
[r,c] = find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1))
r = 5×1
1 3 3 1 2
c = 5×1
1 1 2 3 3
arr = rgb(:,:,1);
%repeated values
arr(sub2ind(size(rgb,[1 2]),r,c))
ans = 5×1
4 1 2 1 2
What should be the output here?
1 and 2 appear more than once in the above list, so do you want the [r,c] values for all the 1 and all the 2? i.e.
[3 1; 1 3] %for 1
[3 2; 2 3] %for 2
Exactly. However, in the following way:
r = [3; 3; 1; 2]
c = [1; 2; 3; 3]
I agree with @Dyuman Joshi here. This seems to all be part of a long composite thread about either selecting scattered pixels from a color image and getting a list of their color tuples, and it's morphed into a convoluted mess of seemingly unnecessary cell arrays.
Maybe it's all unrelated, and maybe it's hard to be certain from the outside perspective, but it sure smells like we're dealing with consequences of questionable decisions which need to be re-evaluated before we wind up digging a hole.
@Alberto Acri, Here's an approach -
rgb(:,:,1) =[4 4 1
3 1 2
1 2 4];
rgb(:,:,2) =[4 4 1
3 3 2
1 2 4];
rgb(:,:,3) =[4 1 1
4 1 2
1 2 2];
rgb = uint8(rgb);
%find the linear indices
linx = find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1));
arr = rgb(:,:,1);
%values
val = arr(linx)
val = 5×1
4 1 2 1 2
%Get unique values and their indices
[vec, ~, ia] = unique(val, 'stable'); % Stable keeps it in the same order
%frequency of the unique values
bin = accumarray(ia, 1);
%Get indices of non-repeating values
[~,uniqueidx] = intersect(val,vec(bin<=1));
%Get the indices of repeating values by getting the difference
repeatidx = setdiff(1:numel(val),uniqueidx);
%Convert the corresponding repeating linear indices to subscripts
[r,c] = ind2sub(size(arr), linx(repeatidx))
r = 4×1
3 3 1 2
c = 4×1
1 2 3 3

Sign in to comment.

I have taken dummy data instead of random inorder to get right co-ordinates, please let me know if you wanted to get same co-ordinates or different.
rgb = randi(255,3,3,3,'uint8')
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 235 202 208 136 3 50 20 50 245 rgb(:,:,2) = 215 53 227 214 246 143 183 49 101 rgb(:,:,3) = 216 11 108 119 131 219 199 236 125
out = mat2cell(rgb,ones(1,size(rgb,1)),ones(1,size(rgb,2)),3);
rgb = uint8([...
100, 100, 100; 150, 150, 200; 255, 255, 255;
100, 150, 200; 200, 200, 200; 50, 50, 50;
255, 0, 255; 0, 255, 0; 150, 150, 150]);
rgb = reshape(rgb, [3, 3, 3])
rgb = 3×3×3 uint8 array
rgb(:,:,1) = 100 100 255 150 200 0 255 50 150 rgb(:,:,2) = 100 150 0 150 200 255 255 50 150 rgb(:,:,3) = 100 200 255 200 200 0 255 50 150
out = mat2cell(rgb, ones(1, size(rgb, 1)), ones(1, size(rgb, 2)), 3);
matForm = cellfun(@(x) numel(unique(x)), out);
[rows, cols] = find(matForm == 1);
equal_coords = [rows, cols];
disp(equal_coords)
1 1 3 1 2 2 3 2 3 3

Products

Release

R2021b

Asked:

on 16 Aug 2023

Commented:

on 18 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!