How do I initialize matrix values from an array of indices?

조회 수: 1 (최근 30일)
Joel Stave
Joel Stave 2016년 7월 28일
댓글: Joel Stave 2016년 7월 28일
The following code:
idx = 1:10:100;
img(idx,idx) = 1;
...yields an array with 100 '1' values in it. The result I'm looking for is an array with a value of 1 only at (1,1), (11,11) ... (91,91). The only way I can find to get the result I want is:
for i=1:length(idx)
img(i,i) = 1;
end
Is there some way to do this without the 'for' loop, since they tend to be less efficient? Thanks.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 28일
Use sub2ind
idx = 1:10:100;
ii=sub2ind(size(img),idx,idx)
img(ii) = 1;
  댓글 수: 1
Joel Stave
Joel Stave 2016년 7월 28일
This is what I was looking for, but didn't know how to word the search, so thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by