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

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!

 Réponse acceptée

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 commentaires

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!
You're totally right. I don't know what I was thinking.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by