How can I return the signed maximum absolute value of each column of a matrix?

10 ビュー (過去 30 日間)
Lucas
Lucas 2014 年 6 月 23 日
編集済み: W. Owen Brimijoin 2014 年 6 月 23 日
Hi,
I have a 24x6000000 matrix (A) and I want to create a 1x6000000 matrix (B) out of it which contains the maximum absolute values of each column. But I don't want to lose the + or -.
I tried the following:
for k = A(24, 6000000); if max(A) == max(abs(A)); B = max(A); else B = min(A); end; end;
When I plot the outcome I get only negative values.
How do I make matlab check each column for the if-condition?
Thanks for all replies!

採用された回答

José-Luis
José-Luis 2014 年 6 月 23 日
data = -11 + randi(21,10,50);
nCol = size(data,2);
[your_result idx] = max(abs(data),[],1);
toAdd = [0 cumsum(10.* ones(1,nCol-1))];
idx = idx + toAdd;
your_result(data(idx) < 0) = -your_result(data(idx) < 0)
Note that this will find the first largest element, independently of its sign.
  2 件のコメント
W. Owen Brimijoin
W. Owen Brimijoin 2014 年 6 月 23 日
編集済み: W. Owen Brimijoin 2014 年 6 月 23 日
That's a bit of a convoluted way of going about finding the maximum values in a matrix. Here's a bit simpler (and likely much faster) way to do this:
[~,index] = max(abs(A));
absmax = A(sub2ind(size(A),index,1:size(A,2)));
Edited to get the correct column indices!
José-Luis
José-Luis 2014 年 6 月 23 日
You're totally right. I don't know what I was thinking.

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

その他の回答 (0 件)

カテゴリ

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