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(:)))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top