What wrong with the indexing code?
古いコメントを表示
If I want to fill in a matrix, of which I have its indices without a for loop, how could I do it? Suppose that the (row(i),col(i)) elements to be filled are given.
value = 189;
row = [ 1 3 6 7 8 ];
col = [ 1 1 4 4 5];
B(row,col) = value
採用された回答
その他の回答 (1 件)
Birdman
2018 年 2 月 8 日
B(sub2ind([max(row) max(col)],row,col)) = value
8 件のコメント
GEORGIOS BEKAS
2018 年 2 月 8 日
sub2ind function converts the equivalent indexes of given rows and columns and it should also know the size of your matrix. The inputs are accordingly given. For instance, the linear index representation for a matrix having 2 rows and 5 columns, the index of the element at
row=2;
col=3;
is
6
GEORGIOS BEKAS
2018 年 2 月 8 日
Birdman
2018 年 2 月 8 日
I did not use for loop here,as you can see.
GEORGIOS BEKAS
2018 年 2 月 8 日
Walter Roberson
2018 年 2 月 8 日
The reshape() is not needed.
Birdman
2018 年 2 月 8 日
Doesn't my answer also work Walter?
GEORGIOS BEKAS
2018 年 2 月 8 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!