Let's pretend that
B=[10 23 32 12 15 18 20] M=true(6) M(B)=false %making the indexed elements false M=or(M,diag(true(size(M,1),1))) %keep the diagonal elements one % creating a matrix which has zero in ith row and ith column and diagonal has ones M1=and(bsxfun(@or,bsxfun(@and,repmat(min(M,[],2),1,size(M,2)),min(M,[],1)),diag(true(size(M,1),1))),M) %Now just multiply M1 with your matrix A, and you are done. newA=A.*M1
You can combine the above two lines in one, but I prefer them to be disjoint for readability.
source share