1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
function ij = find_ijmax(C)
%retourne les indices du max le plus proche du centre de la matrice carrée
%C. Dans le cas de plusieurs max égaux, retourne celui le plus proche, et
%s'il en existe plusieurs au plus proche, en retourne un aléatoirement.
%Find indices of max(C):
Mat = C == max(max(C));
if(sum(sum(Mat)) > 1)
m = round(size(C,2)/2); %center of the matrix.
%recover indexes of maxs
idx = find(Mat(:) == 1);
%[i,j] = ind2sub(size(C,1),idx);
i = rem(idx-1,size(C,1))+1;
j = (idx-i)/size(C,1) + 1;
%Chessboard Chebyshev distance to m:
Cdis = max(abs(m-i),abs(m-j));
%Check for min distances values:
idx = min(Cdis) == Cdis;
%gather indexes:
ijs = [i(idx), j(idx)];
if(size(ijs,1) > 1)
%Several Max at equal distance:
%randomly pick one:
ij = ijs(ceil(rand(1)*size(ijs,1)),:);
end
else
%[i,j] = ind2sub(size(Mat,1),find(Mat));
%ij = [i,j];
%fast replacement of ind2sub:
ij(1) = rem(find(Mat)-1,size(Mat,1))+1;
ij(2) = (find(Mat)-ij(1))/size(Mat,1) + 1;
end |
Partager