multiplication of empty 0X0 double and scalar (1X1)
11 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am trying to calculate sum of certain maximum elements of arrays. When some of these variables are not available in the program (due to if statement), then the maximum value of element of these variable array comes out to be 0X0 double (as I have by default defined the variable as a cell array of zeros).
The problem I am facing is an error "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
I think the error is due to the multiplication part of two variables on the right side of equation i.e. - 0X0 double and one scalar. The other (left) side of equation (sum) is supposed to produce the scalar value.
How to handle this and get the scalar output on left side by ignoring these "absent" variables value (assigned 0 X0 double and a scalar)???
Can anybody help with this small issue and guide me on this, please?
A=cell(1,5);
B=cell(1,5);
C=cell(1,5);
a=1;
b=2;
c=3;
if.......
% A, B or C is not used and modified.
% Say A is not used or modified.
end...
AA=max(A);
BB=max(B);
CC=max(C);
DD=max(D);
sum= max(AA*a+BB*b+CC*c+DD*d);
% AA is now 0X0 double and a is 1 (1X1 double).
% Is this causing problem???
1 件のコメント
Walter Roberson
2021 年 9 月 15 日
You can check with isempty()
However, you cannot take max() of a cell .
採用された回答
the cyclist
2021 年 9 月 14 日
One possible solution is to use a try-catch structure. The "try" block will be the code that is generally executed, but if that section fails (due to the empty array), then the "catch" block will be executed instead.
4 件のコメント
Walter Roberson
2021 年 9 月 15 日
if isempty(sum) ;sum=0; end
do that before you attempt to assign sum into a subscripted variable.
... and don't use "sum" as a variable name. Chances are too high that you will end up also wanting to use the function sum() .And besides, it confuses readers.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!