Info

This question is closed. Reopen it to edit or answer.

how to fix "In an assignment A(I) = B, the number of elements in B and I must be the same"

1 view (last 30 days)
This is the code I wrote to store values in a vector but it is not working for some reason. I have realized that I am adding vector to a scalar but I am not sure how to fix it!
CODE ======================================================================
y1 = 1;
deltT = 1;
for t1=1:1:10;
y1(t1) = y1 + t1*deltT
end
==================================================
ERROR MESSAGE
y1 =
2
y1 =
2 4
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Untitled (line 17)
y1(t1) = y1 + t1*deltT ================================================================================
Help would me much appreciated! Thanks!

Answers (2)

sixwwwwww
sixwwwwww on 11 Oct 2013
You should not use same name for variable and array. You are using "y1" as variable as well as an array later in for loop. So you can do it correctly this way:
y = 1;
deltT = 1;
for t1=1:1:10
y1(t1) = y + t1*deltT
end
or in a better way
y = 1;
deltT = 1;
t1 = 1:10;
y1 = y + t1 * deltT
Good luck!

Hassan
Hassan on 11 Oct 2013
Thanks I actually troubleshooted it a little more and ended up with fixing it. I used this code
y1 = 1;
deltT = 1;
for t1=1:1:10;
y1(t1) = y1(t1-1) + t1*deltT
Again, thank you so much for your help!
end

Community Treasure Hunt

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

Start Hunting!