Error using * matrix dimensions must agree?

45 views (last 30 days)
Please note that I have tried using " .* " instead of " * " and it STILL doesn't work. That is the fix that every single similar question has gotten. Afterwards, it just says MATRIX dimensions must agree instead of INNER MATRIX dimensions must agree. Below is my script:
g = 9.81; t = [15:15:75]; x = [0:5:80]; v = 28; yo = 0;
y = (tan(t))*x - (g/(2*v^2*(cos(t))^2))*x^2 + yo;
It is supposed to generate results that are assembled in an array where the first dimension (rows) corresponds to the x values, and the second dimension (columns) corresponds to the t values. What should I go about doing in order to fix this error of matrix dimensions?
  2 Comments
David Goodmanson
David Goodmanson on 2 Oct 2016
the 'meshgrid' function is designed to give you the x and t arrays that you need.
Stephen23
Stephen23 on 2 Oct 2016
Edited: Stephen23 on 2 Oct 2016
"That is the fix that every single similar question has gotten"
It is not a "fix". The users were using the wrong operator for their algorithm, that is all, and someone told them which operator to use.
It usually boils down to people not knowing about different matrix multiplication methods, not really understanding how they can be used, and not bothering to read the documentation. No matrix multiplication method is a "fix" for another matrix multiplication, they simply do different things. Is log2 a "fix" for log10 ?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 2 Oct 2016
Edited: Stephen23 on 2 Oct 2016
Your question states that 1st dim should correspond to the x, and the 2nd dim to t. But where are you specifying that? Both x and t are row vectors (and of different lengths), so neither of them specify anything along the 1st dim:
>> size(t)
ans =
1 5
>> size(x)
ans =
1 17
Multiply two row vectors of different lengths will be an error, no matter how many different matrix multiply methods you try:
>> tan(t)*x
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> tan(t).*x
??? Error using ==> times
Matrix dimensions must agree.
There is no standard mathematical definition for multiplying row vectors of different lengths. In this example, what should the 4 multiply with?:
>> [1,2,3,4].*[5,6,7]
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Instead of imagining what the code should be doing, try to pay attention to what the code is really doing. For example, when you read about matrix multiplication then you would realize that you can trivially orient the x as a column, and you get a matrix output when it is multiplied with the row vector t:
>> x(:)*tan(t)
ans =
0 0 0 0 0
-4.28 -32.027 8.0989 1.6002 -2.1035
-8.5599 -64.053 16.198 3.2004 -4.207
-12.84 -96.08 24.297 4.8006 -6.3105
... etc
And then this leads directly to one simple solution to your question:
>> g = 9.81;
>> t = 15:15:75;
>> x = 0:5:80;
>> v = 28;
>> yo = 0;
>> y = x(:)*tan(t) - (x(:).^2) * (g./(2*v^2*(cos(t)).^2)) + yo
y =
0 0 0 0 0
-4.551 -38.6 7.5321 1.4278 -2.2876
-9.644 -90.348 13.931 2.5107 -4.9434
-15.279 -155.24 19.196 3.2487 -7.9673
-21.456 -233.28 23.327 3.6419 -11.359
-28.175 -324.47 26.325 3.6903 -15.12
-35.436 -428.81 28.189 3.3937 -19.248
-43.239 -546.29 28.92 2.7524 -23.745
-51.585 -676.92 28.517 1.7661 -28.61
-60.472 -820.7 26.981 0.435 -33.843
-69.901 -977.63 24.311 -1.241 -39.444
-79.872 -1147.7 20.508 -3.2618 -45.414
-90.386 -1330.9 15.571 -5.6275 -51.751
-101.44 -1527.3 9.5002 -8.338 -58.457
-113.04 -1736.8 2.2961 -11.393 -65.531
-125.18 -1959.5 -6.0416 -14.794 -72.973
-137.86 -2195.3 -15.513 -18.539 -80.784
Multiplying vectors (and matrices) is covered quite well in thousands of online high-school math tutorials, so there is no point in repeating it all here. Writing code without understanding what it is doing is a guarantee that the code will be buggy.
  4 Comments
Alexander Babayi
Alexander Babayi on 2 Oct 2016
Edited: Alexander Babayi on 2 Oct 2016
Thank you for answering, although I'm still not understanding something with the following:
x(:)*tan(t)
I noticed when I was writing the script that they are of different lengths, and that it likely had something to do with the error. You said here that making "x" into "x(:)" orients the x as a column. I noticed that in the beginning and tried to define x as " x = [0:5:80]' " with the apostrophe, transposing it (is that correct?). But now that the x is a column being multiplied by a row, doesn't a problem still exist in that the number of elements in each is not equivalent? Shouldn't you only be able to multiply a column by a row if the number of elements in each is the same? Unless the " (:) " part of the code does something to fix that, I'm having trouble understanding how it is functioning now. I tried looking it up here:
https://www.mathworks.com/help/matlab/ref/colon.html
where it said that using the colon "fills" A (the example matrix provided). Does that have anything to do with why it works? Thank you.
EDIT: I remembered that it isn't the number of elements that must be equal, but the number of rows must equal the number of columns.
Alexander Babayi
Alexander Babayi on 2 Oct 2016
Edited: Alexander Babayi on 2 Oct 2016
Please disregard the previous post, my memory of linear algebra is very foggy. Thank you for your help with the code.

Sign in to comment.

More Answers (1)

Jang geun Choi
Jang geun Choi on 2 Oct 2016
I think, if your 'y' is vary with two independent variables, x and t, you should make not one dimensional vector domain but two dimensional matrix domain.
In order to make the matrix domain, you can use 'meshgrid' function.
g=9.81;
t=[15:15:75];
x=[0:5:80];
v=28;
yo=0;
[xm,tm]=meshgrid(x,t);
y=(tan(tm)).*xm-(g./(2.*v^2.*(cos(tm)).^2)).*xm.^2+yo;
pl=plot(x,y);
legend(pl,num2str(t','t=%2.0f'))

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!