MATLAB: How to find maximum value in a matrix

automizationmatrixmaximum

Now, there is reason why I don't want to use a max() or similar built-in function to find the maximum value of a matrix. My question is following:
Imagine, we have a matrix, let's call it 'gauss' and looks like this:
gauss =
0 0.2000 0
0.5000 1.0000 0.6000
0 0.3000 0
What I want is to find the maximum, i.e. 1 or the value at gauss(2,2). I need to automize a step search from gauss(1,1) so that I end up finding gauss(2,2) in this entire matrix.
Would be glad if you could give some idea regarding the coding of the steps.
Thanks!

Best Answer

Demo of local search based on 4-connected neighbourhood:
Demo of local search based on 8-connected neighbourhood:
Code used to generate these figures:
function Gauss_map_demo
% Sampling grid
N=30; % grid size
dx=2*rand(1)-1;
dy=2*rand(1)-1;
x=linspace(-2,2,N)+dx;
y=linspace(-2,2,N)+dy;
ds=x(2)-x(1);
[X,Y]=meshgrid(x,y);
% Random precision matrix (inverse of covariance)
t=rand(1)*pi;
R=[cos(t) -sin(t);sin(t) cos(t)];
D=sort((rand(1,2)+1)/2,'descend');
S=R'*diag(1./D)*R;
% Evaluate (zero-centred) Gaussian on specified grid
P=[X(:) Y(:)]';
G=exp(-0.5*sum(P.*(S*P),1));
G=reshape(G,size(X));
% Visualize
figure('color','w')
imagesc([1 N],[1 N],G)
axis equal
set(gca,'XLim',[0 N+1],'YLim',[0 N+1])
hold on
colorbar;
% Identify corner with smallest value as a starting point; this is done for
% demonstration purposes. In practice, you should select corner with
% largest value, as it will reduce number of steps required to reach
% absolute maximum.
ij=[1 1;N 1;N N;1 N]; % corner indices
id=sub2ind([N N],ij(:,1),ij(:,2));
[~,id_min]=min(G(id));
seed=ij(id_min,:);
% Search using on 4-connected neighbourhood
conn=false; % set conn=true to use 8 connected neighbourhood
[G_max,ij_max,trj]=GetMax(G,seed,conn);
fprintf('Maximum value found %.3f at G(%u,%u) in %u steps\n',G_max,ij_max(1),ij_max(2),size(trj,1))
plot(trj(:,2),trj(:,1),'-r','LineWidth',2); % ascent trajectory
plot(trj(1,2),trj(1,1),'ok','LineWidth',3,'MarkerSize',10); % start
plot(trj(end,2),trj(end,1),'xk','LineWidth',3,'MarkerSize',10); % end
function [G_max,ij_max,trj]=GetMax(G,seed,conn)
% Get maximum value of an image using local search
%
% - G : N-by-M image
% - seed : starting point
% - conn : set conn=true to use 8-connected neighbourhood {default},
% and set conn=false to use 4-connected neighbourhood
if nargin<2 || isempty(seed), seed=[1 1]; end
if nargin<3 || isempty(conn), conn=true; end
siz=size(G);
% Initial value
ij_max=seed;
G_max=G(ij_max(1),ij_max(2));
trj=ij_max;
% Neighbour offsets
ngb_x=repmat([-1 0 1],[3 1]);
ngb_y=ngb_x';
d_ij=[ngb_y(:) ngb_x(:)];
if conn
d_ij(5,:)=[];
else
d_ij([1 3 5 7 9],:)=[];
end
% Search
while true
% Indices of neighbours around ij_max
i_ngb=ij_max(1)+d_ij(:,1);
i_ngb=max(min(i_ngb,siz(1)),1);
j_ngb=ij_max(2)+d_ij(:,2);
j_ngb=max(min(j_ngb,siz(2)),1);
id_ngb=sub2ind(siz,i_ngb,j_ngb);
% Select neighbour with largest value; I am using 'max' function here, but you can loop through the neighbouring values if you want
[g_max,id_max]=max(G(id_ngb));
if g_max>G_max
G_max=g_max;
ij_max_new=[i_ngb(id_max) j_ngb(id_max)];
if nargout>2
trj=cat(1,trj,ij_max_new);
end
ij_max=ij_max_new;
else
break
end
end
Related Question