File Exchange

image thumbnail

absmax

version 1.0 (1.27 KB) by

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

2 Downloads

Updated

View License

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

Comments and Ratings (3)

Jan Simon's solution is more efficient.

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

Jan Simon

Jan Simon (view profile)

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

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

Linear indexing beats the logical indexing here.

Owen Brimijoin

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

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

Hooray for logical indexing!

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video