Index exceeds matrix dimensions error.

조회 수: 1 (최근 30일)
ajk1
ajk1 2015년 4월 21일
댓글: ajk1 2015년 4월 21일
Hi, I would like to know how to change the 0's in C that are in the same position where d1 finds values where (d<=d0) with the corresponding d2 value. d is a 100x1 array and d0 is the mean value. For instance if the first and fourth elements in d are less than d0, then the first and fourth elements in d2 are stored as the first and fourth elements in C respectively.
C=zeros(100,1);
d = randi(10, 100, 1);
d0 = mean(d);
d1=find(d<=d0);
d2 = randi(10, length(d1), 1);
C(d1)=d2(d1);

채택된 답변

Adam
Adam 2015년 4월 21일
You should just be able to replace that last line with
C(d1)=d2;
You have already trimmed d2 to the size of d1 in the previous line.
  댓글 수: 1
ajk1
ajk1 2015년 4월 21일
Thanks Adam! Worked like a charm.

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

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