MATLAB: Error using * Inner matrix dimensions must agree

12,439
theta = 0:(0.1):2*pi;
phi = 0:(0.1):pi;

With the above two lines, you've created two vectors. These are different lengths (since one goes to pi and the other to 2*pi, by the same step size.

You do want to use element-wise multiplication (.*), but you need your vectors to be the same lengths... otherwise, which elements get multiplied together?

Share:
12,439

Related videos on Youtube

harbingeroftuna
Author by

harbingeroftuna

Updated on September 15, 2022

Comments

  • harbingeroftuna
    harbingeroftuna over 1 year

    So I'm working on a function that will receive inputs from from a user-defined structure to plot an ellipsoid, but Matlab keeps spitting out this error. Here's the portion I'm having trouble with:

    theta = 0:(0.1):2*pi;
    phi = 0:(0.1):pi;
    a1 = ellipsoid_in(1).major_axis;
    b1 = ellipsoid_in(1).minor_axis;
    c1 = ellipsoid_in(1).transverse_axis;
    
    x1 = a1*sin(phi)*cos(theta);
    y1 = b1*sin(phi)*sin(theta);
    w1 = c1*cos(phi);
    plot3(x1,y1,w1)
    grid on
    hold on
    x2 = x1;
    y2 = y1;
    w2 = w1;
    plot3(x2,y2,z2)
    xx = [x1;x2];
    yy = [y1;y2];
    ww = [w1;w2];
    

    The error is occurring in my first (x1) equation and I've already tried using the .* operator on them all with the same result. I'm guessing the problem is coming from the 1x2 structure I'm calling, but I don't know how to fix it. The variables for the structures all correspond to scalars. Any help is much appreciated.

    • tmpearce
      tmpearce over 9 years
      If you've really used the .* operator instead of *, you wouldn't continue to get the same error - you could get other errors, but not that one, since .* doesn't do matrix multiplication, it does element-wise multiplication. So... what you you mean by "same result"? Also, what is the type and size of a1?
    • David
      David over 9 years
      To diagnose "matrix dimensions must agree" errors, put size commands of each part of the equation on the lines before it, then it's obvious what the problem is. (i.e. do size(a1), size(phi), size(theta) before x1= ... and look at the output).