Matrix manipulation

조회 수: 5 (최근 30일)
Pedro Cavaco
Pedro Cavaco 2011년 4월 12일
Hi,
I have a matrix A(1000,6) and i want to write 1's at certain positions say:
i = [785 58 595 389 450 956];
j = [4 6 2 3 5 1];
If I do A(i,j) = 1 it places 1's in the positions "i" of all the columns "j".
But what i want is:
A(i(1),j(1)) = 1
A(i(2),j(2)) = 1
...
A(i(end),j(end)) = 1
Does anybody knows how to do this without having to use a for loop?
  댓글 수: 1
Teja Muppirala
Teja Muppirala 2011년 4월 12일
Is there a compelling reason not to use a FOR loop? A loop will probably do this faster than any non-loop solution (that I can think of anyways).

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 4월 12일
variant
i = [785 58 595 389 450 956];
j = [4 6 2 3 5 1];
A(sub2ind(size(A), i, j)=1;

추가 답변 (0개)

카테고리

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