I don't understand why the matrix dimensions are exceeded
Show older comments
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
on 16 Feb 2015
Accepted Answer
More Answers (1)
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
on 16 Feb 2015
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!
Categories
Find more on Creating and Concatenating Matrices 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!