Matrix dimensions must agree
4 views (last 30 days)
Show older comments
Hello... I use {air_sea ToolBox } So when I Run it , I get this error (Matrix dimensions must agree.),How can I fix them . (error message is >>> Error using * Inner matrix dimensions must agree.)..
3 Comments
KSSV
on 29 Nov 2016
This error occurs, mostly when you are trying to multiply two matrices which don't obey multiplication condition. Go to the specific line where error pops out, and check the dimensions of the matrices.
Jan
on 5 Dec 2016
@b.m.n n: As James has said before: We need the complete message. There is also a part, which tells the failing line. It is not efficient, if we guess, where the error occurres.
Answers (1)
Guillaume
on 29 Nov 2016
There are a few things that are confusing in your code:
lwest = ...
...; clear all
So you wipe out all the variables including the lwest. What was the point in calculating it in the first place, then. Is there actually a need for the clc; close all; clear all or is it just something you always write regardless of if it's needed or not?
Tsea = A(:,2); %so Tsea is a column vector, size Nx1
TE = Tsea(E,:); %since Tsea has only one column, : is just that one column.
So why the :? Was Tsea expected to contain more than one column or is it just redundant and there to confuse the reader?
TE = Tsea(E); %would work just the same and is less confusing
It's not clear where your error is coming from since you haven't told us which line is causing the error, and there does not appear to be any matrix multiplication in your code. However, the following line is suspicious
e_a=e_a.';%795*1
The comment is wrong. Since e_a is a function of TE it starts as a column vector, and the above line transposes it to a row vector, so it'll be size 1xN, not Nx1.
Moreover, in your last line you have:
something = f(TE) .* f(e_a) .* Fc
something = 795x1 .* 1x795 .* Fc (12x1? according to earlier comment)
The first part of the multiplication will only work in R2016b, in earlier version it will produce an error. In R2016, it will create a 795 x 795 matrix. Whichever version, if Fc is indeed 12x1, it can't be multiplied with a 795 x 795 matrix.
2 Comments
Guillaume
on 5 Dec 2016
My point about the : wasn't with the line Tsea = A(:, 2), that is fine and makes Tsea a column vector. It was with the line TE = Tsea(E, :). The : selects all column, but by definition Tsea has only one column, so the colon is redundant and the line is equivalent to TE = Tsea(E). This raises the question: is there a bug? Did the author meant to select more than one column of something but used the wrong variable?
As stated, the line
lwest = -emiss_lw*sigmaSB.*TE.^4.*(0.39 - 0.05*sqrt(e_a)).*Fc;
is never going to work if FC is 12x1 and TE is 795x1, as you can't multiply matrices of incompatible size. In versions prior to R2016b, the line would also fail because you're doing a memberwise multiplication of TE, a 795x1 matrix, with e_a, a 1x795 matrix. It will work in R2016b because of the new implicit expansion, but will result in a 795x795 matrix which may not be the intent.
See Also
Categories
Find more on Logical 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!