フィルターのクリア

What wrong with the indexing code?

2 ビュー (過去 30 日間)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 2 月 8 日
コメント済み: GEORGIOS BEKAS 2018 年 2 月 8 日
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

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 8 日
B = full( sparse(row, col, value) );
or
B = accumarray([row(:), col(:)], value);
  2 件のコメント
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 2 月 8 日
how about using arrayfun/bsxfun or something?
Walter Roberson
Walter Roberson 2018 年 2 月 8 日
Not at all easily with arrayfun or bsxfun as assignment through function calls is difficult and never less than ugly.

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

その他の回答 (1 件)

Birdman
Birdman 2018 年 2 月 8 日
B(sub2ind([max(row) max(col)],row,col)) = value
  8 件のコメント
Birdman
Birdman 2018 年 2 月 8 日
Doesn't my answer also work Walter?
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 2 月 8 日
it did not :/

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by