determine within cell the coordinates (rows/columns) having equal 3 numbers
Mostrar comentarios más antiguos
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);
Respuesta aceptada
Más respuestas (2)
You don't need to convert the data to cell array to find that -
rgb = randi(4,3,3,3,'uint8')
%Method 1
[r,c]=find(rgb(:,:,2)==rgb(:,:,1) & rgb(:,:,3)==rgb(:,:,1))
%Method 2
[r,c]=find(all(rgb(:,:,2:3)==rgb(:,:,1),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 comentarios
dpb
el 16 de Ag. de 2023
"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...
Dyuman Joshi
el 16 de Ag. de 2023
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.
Alberto Acri
el 17 de Ag. de 2023
Dyuman Joshi
el 17 de Ag. de 2023
I'm sorry, I didn't understand what you said. Please elaborate on your query.
Alberto Acri
el 17 de Ag. de 2023
Dyuman Joshi
el 17 de Ag. de 2023
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"
Alberto Acri
el 17 de Ag. de 2023
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))
arr = rgb(:,:,1);
%repeated values
arr(sub2ind(size(rgb,[1 2]),r,c))
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
Alberto Acri
el 17 de Ag. de 2023
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)
%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))
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')
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])
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)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!