multiplication of empty 0X0 double and scalar (1X1)

조회 수: 11 (최근 30일)
Rohit Mangalekar
Rohit Mangalekar 2021년 9월 14일
댓글: Rohit Mangalekar 2021년 9월 17일
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
Walter Roberson 2021년 9월 15일
You can check with isempty()
However, you cannot take max() of a cell .

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

채택된 답변

the cyclist
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
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.
Rohit Mangalekar
Rohit Mangalekar 2021년 9월 17일
Thank you both @the cyclist and @Walter Roberson. I changed the logic I was trying to use and it worked.
I just didnt know exactly how this isempty could be used.
isempty is what I used , to check if the AA,BB,CC (the maximum array) are vacant. If they are null, then I assigned them a scalar 0. Because multiplying other scalar (a,b,c) with null vector was causing a problem , as I said in the question itself. If any of the AA,BB or CC was empty- the single equation- as given in the original question/query by me, does not work due to this error. So as @the cyclist said, I would have required to add multiple conditions either using try-catch or if-else. Both created large number of cases. So I avoided them.
Thank you both of you.
P.S. the max function is giving what I actually wanted i.e. the array made of maximum values from each all the columns of the cell array(converted to matrix using cell2mat) not the maximum array. So I didnt have any problem using max on cell arrray.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by