简体   繁体   中英

Find one non-diagonal minimum element from a matrix

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.

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
for k=1:size(A,1)
    A(k,k) = inf;
end
[row,col] = find(A==min(A(:)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM