Hello,
I have a matrix with the values of either zero or one; I want to find non zero elements in every coloumn and locate the indices of each non zero element in every coloumn of a new matrix. For example, let's say my original matrix looks like this:
a =
0 1 1
0 0 1
1 1 1
I want the indices of non zero element in a new matrix look like this:
b =
0 1 1
0 0 2
3 3 3
How can I do it? I tried with the find function but without any success.
Thanks in advance,
Tilek

 採用された回答

KSSV
KSSV 2020 年 2 月 10 日
編集済み: KSSV 2020 年 2 月 10 日

0 投票

iwant = zeros(size(A)) ;
for i = 1:size(A,1)
if any(A(i,:))
iwant(i,A(i,:)~=0) = i ;
end
end

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 2 月 10 日

1 投票

Use implicit expansion.
A = [0 1 1; 0 0 1; 1 1 1];
R = (1:size(A, 1)).';
B = A.*R

3 件のコメント

Tilek Zhumabek
Tilek Zhumabek 2020 年 2 月 10 日
it also works. Thanks!
KSSV
KSSV 2020 年 2 月 11 日
+1 ...I tried without using loop....it didn't flash in my mind that time.
KSSV
KSSV 2020 年 2 月 11 日
編集済み: KSSV 2020 年 2 月 11 日
@Tilek Zhumabek this is the best answer compared to mine. You should accept this answer.

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

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

質問済み:

2020 年 2 月 10 日

編集済み:

2020 年 2 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by