Matrix dimensions must agree.
Show older comments
Kindly suggest me to solve this
Error in MATLABAnalysis>getValues (line 63)
healthIndicator = ((selectedFeatures{:,:} - T.meanTrain{failureMode}) ./ T.sdTrain{failureMode} * T.pcaCoeff{failureMode}(:, 1));
6 Comments
Christopher Stapels
on 21 Jan 2021
Can you give us more of the code and is there anything else in the error message?
Daniel Pollard
on 21 Jan 2021
Posting the function is nearly meaningless when you don't tell us what inputs you're using... What have you tried so far to figure out which matrices don't have agreeing dimensions?
I suspect it's likely coming from either the subtraction, so selectedFeatures{:,:} has the wrong dimensions, or the multiplication/division, so T.meanTrain{failureMode}, T.sdTrain{failureMode} or T.pcaCoeff{failureMode}(:, 1) has the wrong dimensions.
Paul Hoffrichter
on 22 Jan 2021
Check out these values before the healthIndicator line.
size( selectedFeatures{:,:} )
size( T.meanTrain{failureMode} )
size( T.sdTrain{failureMode} )
size( T.pcaCoeff{failureMode}(:, 1) )
size( T.sdTrain{failureMode} * T.pcaCoeff{failureMode}(:, 1) )
These results may clear up the problem for you. There are variations on what is allowed. At the moment, it is unclear whether you have matrices, vectors, or scalars. If you show the results of the above size's, that should help.
Walter Roberson
on 22 Jan 2021
are you using matlab older than R2016b?
Aniket Manjare
on 22 Jan 2021
Aniket Manjare
on 22 Jan 2021
Answers (2)
In order to subtract one matrix from another they must be the same size (same number of rows and columns). The error is telling you that you are trying to subtract one matrix from another but the matrices do not have the same number of rows and columns.
I suggest putting a breakpoint using the debug tool just before that line. Then when the code reaches the breakpoint look at selectedFeatures{:,:} and T.meanTrain{failureMode}, to see what size they actually are. For example, you can type selectedFeatures{:,:} and T.meanTrain{failureMode} on the command line and just see what size they are, or you can look in the Workspace tab or maybe mouse over the variables. Once you see what size they are you will probably get some insight into what's gone wrong. You will then have to fix your code so that both will be the same size
If you haven't used the MATLAB debugger, I would definitely recommend you learn how, here's a link https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
Paul Hoffrichter
on 22 Jan 2021
As Jon pointed out, you can subtract two matrices if they are "the same size (same number of rows and columns)"
It is also possible to subtract a scalar (1x1 matrix) from any matrix:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> s = -10;
>> S = A - s
S =
11 12
13 14
15 16
Also, it is sometimes possible to subtract a vector from a matrix:
>> B
B =
0 -10
>> size(B) % Notice that the size(B,2) == size(A,2)
ans =
1 2
>> C = A - B
C =
1 12
3 14
5 16
>> size(C)
ans =
3 2
Or, subtract a vector from a matrix like this:
>> BB = -[ 10 20 30]'
BB =
-10
-20
-30
>> size(BB) % Notice that the size(BB,1) == size(A,1)
ans =
3 1
>> CC = A - BB
CC =
11 12
23 24
35 36
>> size(CC)
ans =
3 2
1 Comment
Good point that there are some other valid subtraction (and similarly) addition operations defined for some cases where the matrix sizes are unequal. They can all be thought of as ways to first convert the two matrices to equal size matrices and then perform a standard linear algebra element by element subtraction.
This "implicit expansion" was extended a good deal in R2016b (already a long time ago I guess). This is very convenient, but also has the downside of not always identifying errors where the user is unintentionally operating on different size matrices. The trickiest one for me is subtracting a length m column from a length n row which gives a m by n result. It is easy to lose track of whether vectors are rows or columns, and in the case where you intend to subtract a length n vector from another length n vector and one of them happens to be a row you may not expect to get a n by n matrix. There is a good discussion of all of this in https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b/
Communities
More Answers in the ThingSpeak Community
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!