About maximum value in matrix

3 ビュー (過去 30 日間)
Mohamad Agung Prawira Negara
Mohamad Agung Prawira Negara 2018 年 8 月 6 日
コメント済み: Stephen23 2018 年 8 月 7 日
I have a problem regarding finding a maximum value from my matrix (largest element in the row)
I have matrix A shown bellow
I know that I can use
M=max(A,[],2)
Then I will get matrix M such that
But the answer I want is not that, what I want is that . The one that I want is
The answer that I want is the maximum value of each row ignoring the negativity of each value like shown in the matrix above.
I hope my question is clear enough.

採用された回答

Stephen23
Stephen23 2018 年 8 月 6 日
編集済み: Stephen23 2018 年 8 月 6 日
This can be done using sub2ind:
A = [2,4,-1,0,-1;1,3,12,-3,2;-12,2,4,0,-1;2,4,0,-5,-2;3,-4,-1,0,8]
[~,C] = max(abs(A),[],2); % C = column indices
S = size(A);
R = 1:S(1); % R = row indices
X = sub2ind(S,R(:),C(:)); % X = linear indices
Z = A(X)
Giving
Z =
4
12
-12
-5
8
It would be nice if max etc. could return the linear indices, but that is what we have to work with. Note that you can also calculate the linear indices yourself, without sub2ind:
X = S(1)*(C-1)+(1:S(2)).'
  2 件のコメント
Mohamad Agung Prawira Negara
Mohamad Agung Prawira Negara 2018 年 8 月 6 日
Thanks for the answer, it works for me
Stephen23
Stephen23 2018 年 8 月 7 日
Another possibility:
>> A = [2,4,-1,0,-1;1,3,12,-3,2;-12,2,4,0,-1;2,4,0,-5,-2;3,-4,-1,0,8];
>> [C,X] = max(max(cat(9,A,-A),[],2),[],9);
>> C .* (3-2*X)
ans =
4
12
-12
-5
8

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2018 年 8 月 6 日
use abs() and note that the second return of max() gives the indices. See if you can figure it out.
  1 件のコメント
Mohamad Agung Prawira Negara
Mohamad Agung Prawira Negara 2018 年 8 月 6 日
thanks for the hint

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by