필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by