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. 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
    #1
    1. Advertisements

  2. seth patterson

    Joseph Guest

    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
    #2
    1. Advertisements

  3. seth patterson

    Loren Shure Guest

    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
    #3
  4. seth patterson

    Matt J Guest

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

    Because the left side w(k) is a scalar and the right hand side n(k)*u is not.
     
    Matt J, Oct 12, 2010
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.