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

9 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

José-Luis
José-Luis am 23 Jun. 2014
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 Kommentare
W. Owen Brimijoin
W. Owen Brimijoin am 23 Jun. 2014
Bearbeitet: W. Owen Brimijoin am 23 Jun. 2014
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 am 23 Jun. 2014
You're totally right. I don't know what I was thinking.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by