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

12 views (last 30 days)
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!

Accepted Answer

José-Luis
José-Luis on 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 Comments
W. Owen Brimijoin
W. Owen Brimijoin on 23 Jun 2014
Edited: W. Owen Brimijoin on 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!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!