determine within cell the coordinates (rows/columns) having equal 3 numbers
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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);
0 Commenti
Risposta accettata
dpb
il 16 Ago 2023
[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.
0 Commenti
Più risposte (2)
Dyuman Joshi
il 16 Ago 2023
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 Commenti
Dyuman Joshi
il 18 Ago 2023
@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))
C B
il 16 Ago 2023
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)
0 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!