Matrix Indexing

조회 수: 11 (최근 30일)
Joerg
Joerg 2011년 2월 4일
I have an index matrix.
idx = [1 5 ; 3 7 ]
The goal is to get a matrix like this:
1 0
0 0
0 1
0 0
1 0
0 0
0 1
Indexing the matrix like
a(idx) = 1;
doesn't work. Right now I use a loop, but I am not sure if this is the best way.

채택된 답변

Matt Fig
Matt Fig 2011년 2월 4일
A = zeros(max(idx(:)),size(idx,1));
A(sub2ind(size(A),idx,cumsum(ones(size(idx))))) = 1
  댓글 수: 1
Joerg
Joerg 2011년 2월 7일
Thanks!

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

추가 답변 (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