MATLAB Answers

Matrix dimensions must agree.

59 views (last 30 days)
Aniket Manjare
Aniket Manjare on 21 Jan 2021
Edited: Jon on 22 Jan 2021
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

Show 3 older comments
Walter Roberson
Walter Roberson on 22 Jan 2021
are you using matlab older than R2016b?
Aniket Manjare
Aniket Manjare on 22 Jan 2021
no Actually i am using MatlabAnalysis in ThingSpeak
Aniket Manjare
Aniket Manjare on 22 Jan 2021
When i run it again it shows the message like
That the minus sign "-", whats wrong in the minus sign in below code, can you please help me
Error using -
Matrix dimensions must agree.
Error in analysis>getValues (line 67)
healthIndicator = ((selectedFeatures{:,:} - T.meanTrain{failureMode}) ./
T.sdTrain{failureMode} * T.pcaCoeff{failureMode}(:, 1));

Sign in to comment.

Answers (2)

Jon
Jon on 22 Jan 2021
Edited: Jon on 22 Jan 2021
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

  0 Comments

Sign in to comment.


Paul Hoffrichter
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

Jon
Jon on 22 Jan 2021
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/

Sign in to comment.

Products

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!