Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
WHEN I RUN THIS CODE IT AGAIN AND AGAIN GIVE THIS PROBLEM THAT IS 'Index exceeds matrix dimensions.'
조회 수: 3 (최근 30일)
이전 댓글 표시
n=('enter the number of rows');
m=('enter the number of columns');
Pmin=[100 50 100];
Pmax=[600 200 400];
PopM=[];
for i = 1:n
for j = 1:m
PopM(i,j) = Pmin(j) + rand * (Pmax(j)-Pmin(j));
end
end
PopM
??? Index exceeds matrix dimensions.
댓글 수: 0
답변 (1개)
kjetil87
2013년 8월 17일
편집: kjetil87
2013년 8월 17일
You are declaring PopM as
PopM=[];
Then your are telling matlab to acces element i and j of this matrix, but this matrix as the size of 0x0.
PopM(1,1)
??? Index exceeds matrix dimensions.
Instead preallocate with
zeros(n,m)
You will also get this error if m>3 in your code, since
numel(Pmin) is 3
numel(Pmax) is 3
댓글 수: 2
Jan
2013년 8월 17일
The assigment of PopM(1,1) is correct and works without problems, although a proper pre-allocation would be more efficient. The problem will appear for m>3 only, when the sizes of Pmin or Pmax are exceeded.
kjetil87
2013년 8월 18일
Yes , you are correct was too quick there =)
Indeed you are not accessing PopM but instead reallocating it every iteration, good you caught that one!
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!