changing a value in an array with matrix indices

조회 수: 2 (최근 30일)
Metin
Metin 2013년 8월 27일
hello all, I want to make such that
if true
i_i=zeros(48,1);
a_a=[3 9 11;25 2 4;5 7 1];
i_i(a_a(2,1) a_a(2,2) a_a(2,3))=1
end
it gives error of Unexpected MATLAB expression. if anyone knows please help thanks
  댓글 수: 1
dpb
dpb 2013년 8월 27일
It's a problem w/ the interface but please edit your question to remove the 'if true' and 'end' wrapper on your code. It shows up when one uses the CODE button w/ a blank area. It's simpler to just type two blanks in front of the first line of code instead...

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

채택된 답변

Roger Stafford
Roger Stafford 2013년 8월 27일
You need to replace
i_i(a_a(2,1) a_a(2,2) a_a(2,3))=1
with
i_i([a_a(2,1),a_a(2,2),a_a(2,3)]) = 1;
(I assume you want to set the 25th, the 2nd, and the 4th elements of i_i to 1.)
  댓글 수: 2
Metin
Metin 2013년 8월 27일
thanks:)
dpb
dpb 2013년 8월 27일
But note in your case since it's the 2nd row, can use the colon operator and write just
i_i(a_a(2,:))=1;
instead.

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

추가 답변 (1개)

dpb
dpb 2013년 8월 27일
Other than you preallocated a 1D (column) array and then wrote access to it w/ three subscripts, the expression
i_i(a_a(2,1) a_a(2,2) a_a(2,3))=1;
would expand to be
i_i(25 2 4)=1;
That clearly isn't proper array addressing syntax as it's missing the commas between the three locations.
Now it comes to me that perhaps you intended to address the three elements in the vector instead of a 3D expression is more likely -- in that case what you need is to turn the row of a_a into a vector itself.
Explicitly it could be written as
i_i([a_a(2,1) a_a(2,2) a_a(2,3)])=1;
but since it's the 2nd row, use the colon operator and write
i_i(a_a(2,:))=1;
instead.

카테고리

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