Set same elements in a matrix to zero

14 ビュー (過去 30 日間)
WEN SHIN LU
WEN SHIN LU 2022 年 4 月 25 日
回答済み: Matt J 2022 年 4 月 25 日
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?
  2 件のコメント
Jan
Jan 2022 年 4 月 25 日
What is the wanted output for this example?
WEN SHIN LU
WEN SHIN LU 2022 年 4 月 25 日
sorry didn't explain clearly. The output can be:
A = [0 0 0 01 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
I want to keep just one repeated element in its neighborhood.

サインインしてコメントする。

採用された回答

Matt J
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]
A = 3×20
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))
B = 3×20
0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 5 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

その他の回答 (1 件)

Matt J
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]
A = 3×20
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
B = 3×20
0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 5 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by