MATLAB: Error using * Inner matrix dimensions must agree
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?
Related videos on Youtube
harbingeroftuna
Updated on September 15, 2022Comments
-
harbingeroftuna 8 monthsSo 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 over 8 yearsIf 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 ofa1? -
David over 8 yearsTo diagnose "matrix dimensions must agree" errors, putsizecommands of each part of the equation on the lines before it, then it's obvious what the problem is. (i.e. dosize(a1),size(phi),size(theta)beforex1= ...and look at the output).
-