フィルターのクリア

Remove duplicate 1s from matrix columns

1 回表示 (過去 30 日間)
Mitchell Crem
Mitchell Crem 2021 年 1 月 24 日
コメント済み: Image Analyst 2021 年 1 月 25 日
Suppose I have the following logical matrix,
qq =
3×5 logical array
1 0 1 1 1
1 1 0 1 1
1 0 0 0 1
I'm trying to write a few lines which would allow me to set each column to possess only a single 1 by setting additional 1s to 0. The output should look like the following.
qq =
3×5 logical array
1 0 1 1 1
0 1 0 0 0
0 0 0 0 0
:)

回答 (1 件)

Image Analyst
Image Analyst 2021 年 1 月 25 日
Mitchell, try this:
qq = logical([...
1 0 1 1 1
1 1 0 1 1
1 0 0 0 1])
[rows, columns] = size(qq);
for col = 1 : columns
highestOne = find(qq(:, col), 1, 'first');
if ~isempty(highestOne)
qq(highestOne + 1 : end, col) = false;
end
end
qq % Show in command window.
You see:
qq =
3×5 logical array
1 0 1 1 1
1 1 0 1 1
1 0 0 0 1
qq =
3×5 logical array
1 0 1 1 1
0 1 0 0 0
0 0 0 0 0
  2 件のコメント
Mitchell Crem
Mitchell Crem 2021 年 1 月 25 日
Looks good, but I was looking for something vectorised. Apologies, I should've specified that.
Image Analyst
Image Analyst 2021 年 1 月 25 日
And I gave it to you. Saying qq(:, col) means to get the column vector from the matrix.

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

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by