Code covered by the BSD License  

Highlights from
absmax

Be the first to rate this file! 9 Downloads (last 30 days) File Size: 1.27 KB File ID: #41115

absmax

by

 

Returns the value of the element with the largest absolute value in the input matrix.

| Watch this File

File Information
Description

Returns the value of the element with the largest abs value in the input matrix 'A'. Input 'A' must be numeric, but can be any size and shape.

This is useful because it eliminates iterating through all the dimensions of a matrix.

examples:
A=[-5 3 2 3; 3 2 1 4];
absmax(A) will return -5

A=[643,10];
absmax(A) will return 643

Required Products MATLAB
MATLAB release MATLAB 8.1 (R2013a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
19 Aug 2013 Adam McNeilly

Jan Simon's solution is more efficient.

W. Owen Brimijoin's solution returns a matrix when there is more than one max value.

05 Apr 2013 Jan Simon

Even more efficient than W.Owen Brimijoin's suggestion:

[dummy, index] = max(abs(A(:));
result = A(index);

Linear indexing beats the logical indexing here.

05 Apr 2013 W. Owen Brimijoin

What about this alternate (and arguably simpler) means of solving the problem:

A(abs(A)==max(abs(A(:))))

Hooray for logical indexing!

Contact us