Created
August 9, 2014 17:42
-
-
Save kmundnic/47637e8d8e118cefb2de to your computer and use it in GitHub Desktop.
argmin function for Matlab® vectors, matrices, and datacubes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ arg_min, minimum ] = argmin( A, dim ) | |
%ARGMIN Find the argmin of a vector, matrix or datacube. | |
% A is the vector, matrix or data cube for which we want to find the | |
% minimum and the argument of the minimum. | |
% dim is the dimension of the data structure. | |
%% Error check for arguments | |
if dim > 3; error('Dimension must be 1 =< dim =< 3'); end | |
if dim < 1; error('Dimension must be 1 =< dim =< 3'); end | |
if isnan(A); error('A must be real'); end | |
if isinf(A); error('A must be real'); end | |
if ~isreal(A); error('A must be real'); end | |
if dim == 1 | |
[minimum,arg_min] = min(A); | |
return | |
elseif dim == 2 | |
[minNxN,argminNxN] = min(A); | |
[min1xN,argmin1xN] = min(minNxN); | |
minimum = min1xN; | |
arg_min = [argminNxN(argmin1xN) argmin1xN]; | |
return | |
elseif dim == 3 | |
[minNxNxN,argminNxNxN] = min(A); | |
[minNxNx1,argminNxNx1] = min(minNxNxN); | |
[minNx1x1,argminNx1x1] = min(minNxNx1); | |
minimum = minNx1x1; | |
arg_min = [argminNxNxN(1,argminNxNx1(argminNx1x1),argminNx1x1) ... | |
argminNxNx1(argminNx1x1) argminNx1x1]; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment