determine within cell the coordinates (rows/columns) having equal 3 numbers
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
dpb
2023-8-16
[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 个评论
更多回答(2 个)
Dyuman Joshi
2023-8-16
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 个评论
DGM
2023-8-17
编辑:DGM
2023-8-17
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.
Dyuman Joshi
2023-8-18
@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
2023-8-16
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!