Update matrix, single column per row where row index is in vecor
问题 Is there a way to update different column in each row of matrix, where row indices are stored in vector. Example mx = zeros(10,10); cols = [2 3 5 4 6 8 9 1 2 3]'; for i = 1:size(mx,1) mx(i,cols(i)) = 1; end mx produces 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 The question is, whether I can do it without the for loop? 回答1: You can