Find minimum and maximum from same matrix

I have a matrix of 1181*101 that is a Power of an electric vehicle. It has positive, negative and zeros. Now I need to choose minimum of positive from total matrix and maximum of negative from the same total matrix. How can I do it?
Example
i=1181, j=101
% Power =[1181*101] containing both positive, negative and zeros.
% Now,
if Power(i, j) >= 0
[P, index] = min(Power, [], 2);
else
%(i.e. Power(i,j)<0)
[P, index] = max(Power, [], 2);
But every time, either it is taking only minimum or only maximum at a time. I tried changing negative terms to zero for minimum and positive terms to zero for maximum, but it still did not work. Please help me.
I can email to you if you require more details like code and variables.

3 件のコメント

Jan
Jan 2021 年 7 月 14 日
編集済み: Jan 2021 年 7 月 14 日
Do the zeros belong to the negativ or positive values?
Image Analyst
Image Analyst 2021 年 7 月 14 日
You can attach the variables right here. Why not make it easy for people to help you, not hard? Why make us go through an extra step to get your matrix when you could have just attached it in a .mat file right here when you originally posted?
Ajit Bashyal
Ajit Bashyal 2021 年 7 月 16 日
This is the attached file. One more question to Jan, can't i write it in the same variable name like minPositive and maxNegative making a same name as minmax or something like that? And yes I need to neglect the zero because it doesnot have any use.

サインインしてコメントする。

 採用された回答

Jan
Jan 2021 年 7 月 14 日

0 投票

I guess, that zeros values should be ignored. Then:
tmpPower = Power;
tmpPower(tmpPower <= 0) = Inf;
[minPositive, index1] = min(tmpPower, [], 2);
tmpPower = Power;
tmpPower(tmpPower > 0) = -Inf;
[maxNegative, index2] = max(tmpPower, [], 2);

その他の回答 (1 件)

David Hill
David Hill 2021 年 7 月 14 日

0 投票

m=yourMatrix;
m(m<=0)=inf;
Min=min(m,[],'all');
m=yourMatrix;
m(m>=0)=0;
Max=-max(abs(m),[],'all');

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2021 年 7 月 14 日

コメント済み:

2021 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by