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

Discussion in 'MATLAB' started by seth patterson, Oct 12, 2010.

1. ### seth pattersonGuest

When I type the following:

theta = [-90:1:0,1:1:90]; theta = pi/180*theta;
phi = 0; phi = pi/180*phi;
n = 0:1:4;
u = sin(theta)*cos(phi); w = ones(length(u));

for k=1:5,
w(k) = n(k)*u;
end

Why do I get this error?:

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

Error in ==> new at 9
w(k) = n(k)*u;

seth patterson, Oct 12, 2010

2. ### JosephGuest

u is the same size as theta and you are trying to assign that value to a single element.

you probably want
for k=1:5,
w(k, = n(k)*u;
end;

don't forget to predefine w

Joseph, Oct 12, 2010

3. ### Loren ShureGuest

Not sure what you are trying to do, but u is a vector since theta is a
vector. Perhaps change to elementwise multiplication?

w(k) = n(k).*u;

I.e., use .* instead of * (matrix multiplication).

Loren Shure, Oct 12, 2010
4. ### Matt JGuest

=======================

Because the left side w(k) is a scalar and the right hand side n(k)*u is not.

Matt J, Oct 12, 2010