I don't understand why the matrix dimensions are exceeded

2 次查看(过去 30 天)
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