In an assignment A(I) = B, the number of elements in B and I must be the same.

Hi, I know this question must have been asked a million times before but I can't find a problem quite like mine. I am running a loop to predict a trajectory where I calculate time t1 from an equation to give a constant. However this t1 varies with every iteration of the program, hence t1(i).
I then want to use this to get t(i) from
t(i)=0:1:(t1(i)-2);
However it is spitting out the statement as in the title. Can someone suggest a way around this?
Thanks.

 Accepted Answer

The expression 0:1:(t1(i)-2) creates a row vector. You cannot store a row vector into the space of a single numeric array element t(i) .
You could consider using cell arrays:
t{i} = 0:1:t1(i)-2;

More Answers (2)

I don't quite know what your intention is, but the left side is a scalar and the right side in general is a vector, could be scalar, could be empty, so an error is thrown.
You need to step back and think about the error. What is the size of
t(i)
From inspection and a little bit of MATLAB knowledge you should realize it will be the same size as i. Since you are likely looping over i, i will be a scalar (1x1) so t(i) will have 1 element independent from the value of i. In other words, on every iteration t(i) will have 1 element.
Okay now look at
0:1:(t1(i)-2)
How big do you think that is going to be? Do you think it is going to have exactly 1 element on every iteration? Do you see where the error is yet? If not lets consider
0:1:n
where n is equal to (t1(i)-2). This obviously has n+1 elements. So for your assignment to work (i.e., the number of element in B and I to be the same) n must be equal to 0, and in turn t1(i)-2 must be zero and t1(i) must be -2. Are you initializing t1 to be a vector of -2's?

4 Comments

t(i) should be a scalar. It is calculated from distance/velocity where the velocity is u(i)*sind(theta(i)). Basically the vertical component of velocity.
I am using 0:1:(t1(i)-2) so that I can plot the positions x,y,z in time from 0 to t(i)-2.
I think I know what is wrong: I am trying to store 'n' amount of arrays for t(i), which Matlab can't do.
Not sure how to get around this...?
I have no idea what you are talking about. What does "n amount of arrays" mean? Is that an nx1 array? I don't know why you would want to save a whole bunch of variables like 0:1:0, 0:1:1, 0:1:2, 0:1:3, ... that just seems like a waste to me.
Sorry I'm not being clear.
A projectile takes a different time to cover a ceratin distance depending on its velocity in the x and y directions.
I am running 5 iterations with 5 different angles of launch and so the times will be different.
I then use these times to calculate a lot of other things.
Does that make more sense?
I was expecting variables like 0:1:2, 0:1:2.4, 0:1:2.45 etc
While that is not going to work because will all be the same array 0:1:2.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!