MATLAB Answers

Matrix dimensions must agree.

조회 수: 68(최근 30일)
Aniket Manjare
Aniket Manjare 21 Jan 2021
편집: Jon 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
Aniket Manjare
Aniket Manjare 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));

댓글을 달려면 로그인하십시오.

답변(2개)

Jon
Jon 22 Jan 2021
편집: Jon 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

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

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

Community Treasure Hunt

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

Start Hunting!

Translated by