How can I print row number to matrix in matlab?

7 ビュー (過去 30 日間)
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 16 日
コメント済み: Berfin Çetinkaya 2022 年 3 月 17 日
How can I print row number to matrix in matlab?
I have a matrix and I want to print the row number on the cells that write 1 in this matrix. How can I do it?
Maybe it will be clearer if I explain with an example.
first row 1 0 1
second row 0 0 1
third row 1 1 0
fourth row 0 1 1
This is my matrix. I want to convert this matrix to this with code:
first row 1 0 1
second row 0 0 2
third row 3 3 0
fourth row 0 4 4
The values of 1 in the second row got the value 2 because they were in the second row, those in the third row got the value 3. 0 values remained the same. How can I do this in a large matrix?
thank u
  2 件のコメント
Jan
Jan 2022 年 3 月 16 日
The question is not clear yet. Numerical matrices have "elements". "Cells" are found in cell arrays.
If your matrix is:
M = rand(2, 3)
what is the wanted result?
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 17 日
Maybe it will be clearer if I explain with an example.
first row 1 0 1
second row 0 0 1
third row 1 1 0
fourth row 0 1 1
This is my matrix. I want to convert this matrix to this with code:
first row 1 0 1
second row 0 0 2
third row 3 3 0
fourth row 0 4 4
The values of 1 in the second row got the value 2 because they were in the second row, those in the third row got the value 3. 0 values remained the same. How can I do this in a large matrix?

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

採用された回答

David Hill
David Hill 2022 年 3 月 17 日
M=randi(2,10,3)-1;%sample matrix
newM=M.*(1:size(M,1))';
  1 件のコメント
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 3 月 17 日
This answer is correct. Thank u so much

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

その他の回答 (1 件)

Stephen23
Stephen23 2022 年 3 月 17 日
M = [1 0 1; 0 0 1; 1 1 0; 0 1 1]
M = 4×3
1 0 1 0 0 1 1 1 0 0 1 1
V = 1:size(M,1);
Z = V(:).*M
Z = 4×3
1 0 1 0 0 2 3 3 0 0 4 4

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by