I don't understand why the matrix dimensions are exceeded

조회 수: 1 (최근 30일)
Jacob Savona
Jacob Savona 2015년 2월 16일
댓글: Star Strider 2015년 2월 17일
A= zeros(3,10);
A(1,:)=[1:10];
B=.5+rand(1,10)*.4;
A(2,:,:)=B;
C=round(1+rand(1,10)*7);
A(3,:,:)= C;
A
team_A= zeros(3,5);
team_B= zeros(3,5);
for d=[1:1:10]
if rem(d,2)==1;
for e=[1:1:10]
if A(2,e)== max(B)
p=e;
team_A(:,2,:)=A(:,p,:);
A(:,p)=[];
end
end
elseif rem(d,2)==0
for f=[1:1:10]
if A(2,f)== max(C);
i=f;
team_B(:,2,:)=A(:,i,:);
A(:,i)=[];
end
end
end
end
Index exceeds matrix dimensions.
Error in JAS_HW4 (line 14)
if A(2,e)== max(B)
  댓글 수: 1
Jacob Savona
Jacob Savona 2015년 2월 16일
A= zeros(3,10); A(1,:)=[1:10]; B=.5+rand(1,10)*.4; A(2,:,:)=B; C=round(1+rand(1,10)*7); A(3,:,:)= C; A
team_A= zeros(3,5); team_B= zeros(3,5); for d=[1:1:10] if rem(d,2)==1; for e=[1:1:10] if A(2,e)== max(B) p=e; team_A(:,2,:)=A(:,p,:); A(:,p)=[]; end end elseif rem(d,2)==0 for f=[1:1:10] if A(2,f)== max(C); i=f; team_B(:,2,:)=A(:,i,:); A(:,i)=[]; end end end end

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

채택된 답변

Star Strider
Star Strider 2015년 2월 16일
I believe I see the problem.
In these lines:
A(:,p)=[];
and
A(:,i)=[];
you’re shortening ‘A’ by one column. If you want empty values in your array, you either have to set them equal to NaN (in a numeric array), or if you decide to use a cell array, it will tolerate empty entries.
I would change them both to:
A(:,p)=NaN;
and
A(:,i)=NaN;
respectively. Your code will run with that change without throwing the error, but you may have more work to do to make it give you the results you want.
  댓글 수: 4
Star Strider
Star Strider 2015년 2월 17일
Jacob Savona’s ‘Answer’ moved here...
Here is my new code: It doesn't go to the ifelse statement when the condition of the if statement isn't met...should be just else?
A= zeros(3,10);
A(1,:,:)=[1:10];
B=.5+rand(1,10)*.4;
A(2,:,:)=B;
C=round(1+rand(1,10)*7);
A(3,:,:)= C;
A
team_A= zeros(3,5);
team_B= zeros(3,5);
for d=[1:1:10]
j=10;
k=9;
if rem(d,2)==1;
for e=[1:1:j]
if A(2,e,:)== max(B)
p=e;
team_A(:,2,:)=A(:,p,:);
A(:,p)=NaN;
end
end
elseif rem(d,2)==0
for f=[1:1:k]
if A(2,f,:)== max(C)
i=f;
team_B(:,2,:)=A(:,i,:);
A(:,i)=NaN;
end
end
end
j=j-2;
k=k-2;
end
Star Strider
Star Strider 2015년 2월 17일
Whether you use ‘elseif’ or ‘else’ there shouldn’t matter. I don’t follow what you’re doing, so I can’t comment on the logic in your code.
The problem is that you closed the first ‘if’ statement with an ‘end’, so it will never enter the ‘elseif’ structure. Eliminate the second ‘end’ and it should work.
Illustrating:
for e=[1:1:j]
if A(2,e,:)== max(B)
p=e;
team_A(:,2,:)=A(:,p,:);
A(:,p)=NaN;
end
end <ELIMINATE THE SECOND ‘end’ HERE

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 2월 16일
Do you remember when you did this:
A(3,:,:)= C;
and made A into a 3D matrix? Well, what are you doing when you do this:
if A(2,e)== max(B)
Why did you not pass in three indexes for the 3D matrix A??? You passed in only 2 instead of 3. Fix that.
Also, don't compare floating point values like that. Why not? See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  댓글 수: 2
Jacob Savona
Jacob Savona 2015년 2월 16일
I switched A(2,e)==max(B) to A(2,e,:)==max(B). That did not work. I'm trying to select a certain column of the second row for matrix A, then put those values into team_A. Then delete those selected values from matrix A.
Image Analyst
Image Analyst 2015년 2월 16일
편집: Image Analyst 2015년 2월 16일
Change the first part of your code to this:
A = zeros(3,10);
A(1,:) = [1:10];
B = 0.5 + rand(1,10) * 0.4;
A(2,:) = B;
C = round(1+rand(1,10)*7);
A(3,:) = C;
A
max(B)
max(C)
Then, you're setting a row of A equal to [], which shortens it by a column, so you can't go up to column 10 like you originally planned - there aren't that many columns anymore because you deleted one!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by