I face the error of "In an assignment A(I) = B, the number of elements in B and I must be the same" when i want to create a loop a matrix. my simple short code given below, plz help me?

4 次查看(过去 30 天)
clc clear x1=5; x2=3; x3=2; for i=1:3 if i==1 x1=6; elseif i==2 x1=7; elseif i==3 x1=8; end p(i)=[x1-x2 x3-x2; x2-x3 x1-x3] end

采纳的回答

Sara
Sara 2014-6-3
p(1) accepts one element only, not an array. you can replace it with:
p{1}=[x1-x2 x3-x2; x2-x3 x1-x3]
or
p=[x1-x2 x3-x2; x2-x3 x1-x3]
depending on what you are trying to do. Are you sure you did not mean p(i) instead of p(1)?
  2 个评论
Sara
Sara 2014-6-3
You can't store a 4 by 2 matrix into one element so you need to use a different data structure. That's why I suggested a cell array. If that works for you, set:
p = cell(3,1);
before the loop on i, and then use p{i} = ... in the loop.
If a cell array is not what you want, you'll have to explain your problem better, e.g., what are you doing with p after the loop and why a cell array does not solve your issue.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by