Set same elements in a matrix to zero
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a large matrix which has duplicate elements in neighborhood.
For example,
A = [0 0 0 01 1 0 0 0 2 2 2 0 0 0 5 5 5 5 5;
0 0 0 1 1 0 0 0 0 0 2 2 0 0 0 5 0 5 0 5;
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 5 0 5 0]
I have to keep just a unique element in an arbitrary position. Could I use loop to solve this problem?
采纳的回答
Matt J
2022-4-25
Without the Image Processing Toolbox,
A = [0 0 0 0 1 1 0 0 0 2 2 2 0 0 0 5 5 5 5 5;
0 0 0 1 1 0 0 0 0 0 2 2 0 0 0 5 0 5 0 5;
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 5 0 5 0]
[I,J,S]=find(A);
IJS=num2cell(splitapply(@(x) x(1,:), [I,J,S],findgroups(S)),1);
[I,J,S]=deal(IJS{:});
B=accumarray([I,J],S,size(A))
0 个评论
更多回答(1 个)
Matt J
2022-4-25
编辑:Matt J
2022-4-25
Something like this, perhaps?
A = [0 0 0 0 1 1 0 0 0 2 2 2 0 0 0 5 5 5 5 5;
0 0 0 1 1 0 0 0 0 0 2 2 0 0 0 5 0 5 0 5;
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 5 0 5 0]
reg=regionprops(A~=0,'PixelIdxList');
B=zeros(size(A));
for i=1:numel(reg),
j=reg(i).PixelIdxList(1);
B(j)=A(j);
end
B
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Computations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!