ERROR Matrix dimensions must agree.

2 次查看(过去 30 天)
afef
afef 2017-5-20
I tried to write this Decision making structure
for i=1:n
if Ea >= '(96.35-inf)'
fprintf('normal\n' );
elseif Minimale{i} >= '(-185.7-inf)' & variance{i} <= '(-inf-59.35]'& Ed2 <= '(-inf-19.95]'
fprintf('normal\n' );
elseif Maximale{i} >= '(200.4-inf)' & variance{i} <= '(-inf-59.35]' & Ed2 <= '(-inf-19.95]'
fprintf('normal\n' );
else
fprintf('abnormal\n' );
end
end
But i got an error message of Matrix dimensions must agree. Can anyone help me please ?
  2 个评论
Star Strider
Star Strider 2017-5-20
Delete the single quotes, and be certain the parentheses match and are not terminated by square brackets.
Also, the ‘-inf’ references make no sense. What do you want to do?
Try this:
elseif Minimale{i} >= (-185.7) & variance{i} <= (-59.35) & Ed2 <= (-19.95)
elseif Maximale{i} >= (200.4) & variance{i} <= (-59.35) & Ed2 <= (-19.95)

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2017-5-20
编辑:Walter Roberson 2017-5-20
You have
Ea >= '(96.35-inf)'
The right hand side is a 1 x 11 character vector.
If class(Ea) is string (MATLAB R2016b or later), then the character vector will be converted to class string, as by Ea >= string('(96.35-inf)') and then since the implicit right hand side string('(96.35-inf)') would be a scalar, you would, if need be, get scalar expansion of the right hand side to the same size as the left, and in that case there would not be any problem with matrix dimensions agreeing, no matter what size of string object Ea was.
However, perhaps Ea is instead a character vector. Then if Ea is empty or a single character, you would get scalar expansion of Ea for comparison against the 1 x 11 character vector '(96.35-inf)' and that would be permitted. If not, then if Ea is a character vector that is exactly 11 characters long then the element-by-element comparison against '(96.35-inf)' would be permitted, creating a 1 x 11 logical vector, which would be implicitly all()'d, and that would be permitted. But if Ea was a character vector of any other length than empty, 1, or 11, then you would be trying to compare two vectors of different lengths, and that would fail with matrix dimensions problems.
However, perhaps Ea is instead a categorical array. If that were the case, then you would have received an error message such as
Relational comparisons are not allowed for categorical arrays that are not ordinal.
Perhaps, then, Ea is numeric. In that case, both sides would be implicitly converted to double, as if double(Ea) >= double('(96.35-inf)') . That would have matching sizes if Ea is empty, scalar, a vector of length 11 (or, R2016b or later, a matrix with 11 columns) and otherwise would fail with matrix dimension errors like you saw.
In short: do not use == to compare character vectors of different sizes: use strcmp() or ismember()

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by