Clear Filters
Clear Filters

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.

1 view (last 30 days)
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.

Answers (1)

Walter Roberson
Walter Roberson on 13 Dec 2017
Xall = X + S(1:16).';
  2 Comments
Yiqing Zhou
Yiqing Zhou on 13 Dec 2017
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.

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!