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

EDIT: Discovered that obj.t should have been expressed as obj.t(obj.i) also. I was under the impression that I could have left that as a vector and the output would have been a vector, but apparently not. Problem solved.
--------------------------------------------------------------------
Hi all,
I'm having some trouble with the above error in a while loop, shown below:
while obj.w(obj.i) < 100
obj.dy(obj.i)=((((obj.Sw(obj.i))^2))^(1/2))*(cos((obj.w(obj.i)*obj.t)+obj.rad));
obj.y = obj.y + obj.dy(obj.i);
obj.i = obj.i + 1;
end
obj.Sw and obj.w are both 1x101 and obj.t is 1x30. Basically, I'm expecting the output to be a single 1x30 vector, representing a turbulence time history.
Can anyone see where I'm going wrong? I've created an identical loop in different programs before and had no previous trouble.
Thanks in advance.
Craig

Answers (1)

Use .^ and .* instead of ^ and . Otherwise, if you don't put the dots before the operator, you're doing a matrix operation instead of an element by element operation.

1 Comment

I used to have element wise multiplication before I started using the loop structure. I thought that having Sw(i)^2 would treat it as a scalar for each iteration?
Just tried it there regardless, but I still get the same error.

This question is closed.

Asked:

on 10 Apr 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!