I don't understand why the matrix dimensions are exceeded

2 views (last 30 days)
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 Comment
Jacob Savona
Jacob Savona on 16 Feb 2015
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

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 16 Feb 2015
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 Comments
Star Strider
Star Strider on 17 Feb 2015
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 on 17 Feb 2015
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

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Feb 2015
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 Comments
Jacob Savona
Jacob Savona on 16 Feb 2015
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 on 16 Feb 2015
Edited: Image Analyst on 16 Feb 2015
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!

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!