How can I return the signed maximum absolute value of each column of a matrix?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lucas
am 23 Jun. 2014
Bearbeitet: W. Owen Brimijoin
am 23 Jun. 2014
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!
0 Kommentare
Akzeptierte Antwort
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
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!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!