max function does not work with zero

5 views (last 30 days)
Hello I am trying to use the max function, but it will not work. I have attached my log file. I have tried using this function maxStcev=max(Stcev'-K,0); (line 52), but I keep getting negative values.
I have then tried using a for loop with if statement, but this results in error: "Index exceeds matrix dimensions.
Error in mccev (line 45) if (M1(1,o)<0)" Can someone see what I am doing wrong? I want t have a vector only with positive values. Regards, Søren

Accepted Answer

Rajanya
Rajanya on 20 Mar 2025
'M1' in the script is a 1000 x 1 array i.e. a column vector with only one column.
'M1(1,o)' tries to extract the array element from row 1 and column 'o' which does not exist since 'o' is varying from 1 to 1000 and there is only one column in 'M1'.
I assume you wanted to extract the 'o'th entry from 'M1' and therefore changing 'M1(1,o)' to 'M1(o,1)' will resolve the error of index exceeding matrix dimensions.
if (M1(o,1)<0) %changed
maxSt(1,o)=0;
else
maxSt(1,o)=M1(o,1); %changed
end
Furthermore, the negative values obtained from the 'max' function are because, when dealing with complex inputs, 'max' operates on the magnitude of the input arguments and returns the one with the largest magnitude - see here. For example, max of complex(-3) [magnitude 3] and complex(2) [magnitude 2] will return complex(-3).
In the script, 'Stcev' is a complex double array dealing with complex numbers. Since, mag(0) < mag(<complex numbers in Stcev>), 'max' returns the negative (real part) complex numbers only as output.
N.B: Even if a complex number has its imaginary part to be 0 (for e.g., 3+0i), it is still complex which can be verified by the 'isreal' function.
To learn more about the functionalities of 'isreal', you can refer its documentation page by executing the following command from MATLAB Command Window:
doc isreal
Hope this would solve your queries. Thanks!

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!