Question

Is there a shortest way to find the minimum of non-diagonal elements of the matrix along with its index in matlab.

If A= [1 2 3; 4 1 3; 4 4 4]; then i want to return the index of the minimum non-diagonal element. Here that will be 2 in the first row and second column. So, I want to return (1,2). Thanks.

Was it helpful?

Solution

For a fully vectorized alternative try

B = (A + diag(Inf(size(diag(A)))));    % put Inf on diagonal
[~,ndx] = min(B(:));                   % get the linear index of the minimum value
[r,c] = ind2sub(size(A),ndx);          % get row, column of corresponding to linear index

OTHER TIPS

for k=1:size(A,1)
    A(k,k) = inf;
end
[row,col] = find(A==min(A(:)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top