how to use the for loop to simplify my array ? But I got the error In an assignment A(:) = B, the number of elements in A and B must be the same.

조회 수: 3 (최근 30일)
Hi , I try to use the matlab to express Xall
X1 = X + S(1,1)
X2 = X + S(2,1)
X3 = X + S(3,1)
X4 = X + S(4,1)
X5 = X + S(5,1)
X6 = X + S(6,1)
X7 = X + S(7,1)
X8 = X + S(8,1)
X9 = X + S(9,1)
X10 = X + S(10,1)
X11 = X + S(11,1)
X12 = X + S(12,1)
X13 = X + S(13,1)
X14 = X + S(14,1)
X15 = X + S(15,1)
X16 = X + S(16,1)
Xall = [X1;X2;X3;X4;X5;X6;X7;X8;X9;X10;X11;X12;X13;X14;X15;X16];
but I don't know how to use for loop to simplify it
I tried the
for n = 1:16
X(n) = X + S(n,1)
end
but it says A(:) = B, the number of elements in A and B must be the same.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 13일
Xall = X + S(1:16).';
  댓글 수: 2
Yiqing Zhou
Yiqing Zhou 2017년 12월 13일
Hi Roberson, It doesn't work.
In my project, S is 16 by 2 double and I try to create the array of Xall from the first column of S and Yall from the second column of S. For example X1 = X + S(1,1) ...
Yn = Y + S(n,2) where Yall = [Y1;Y2;...Yn]
for y, For example X1 = X + S(1,1) ...
Yn = Y + S(n,2) where Yall = [Y1;Y2;...Yn]
I didn't know how to create the array with that element in, for my code I just wrote one by one element and it's not gonna work for tons of tons elements.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by