from
Max or Min position in a matrix
by Damilola Ogunbiyi
Used to obtain the particular position where a maximum or minimum occurs in a matrix.
|
| position=findit(inmat, opt);
|
function position=findit(inmat, opt);
% To obtain the position of either the maximum or minimum element in a vector
%
% INMAT specifies the input matrix
% Use OPT specify your choice of obtaining a maximum or minimum
% Set OPT=1 when trying to obtain the maximum
% Set OPT=2 when trying to obtain the minimum
%
%
% Oluwadamilola (Damie) Martins Ogunbiyi
% University of Maryland College Park
% Department of Electrical and Computer Engineering
% Communications and Signal Processing
% 08-March-2010
% Copyright 2009-2010 Black Ace of Diamonds.
%% Check input arguments
if nargin>2
error('Too many input arguments');
end
if opt<1 || opt>2
error('Choose 1 for max, 2 for min');
end
%% Obtain the position of the maximum
if opt==1
max_a=max(max(inmat));
for i=1:size(inmat,1)
for j=1:size(inmat,2)
if inmat(i,j)==max_a
position=[i,j];
end
end
end
end
%% Obtain the position of the minimum
if opt==2
min_a=min(min(inmat));
for i=1:size(inmat,1)
for j=1:size(inmat,2)
if inmat(i,j)==min_a
position=[i,j];
end
end
end
end
|
|
Contact us at files@mathworks.com