Logical arrays as indices to arrays to convert elements to zero in MATLAB
Logical arrays as indices to arrays to convert elements to zero in MATLAB I am trying to convert some elements of a matrix to zeros depending on a logical array. Suppose we have a random 5 x 5 matrix: 5 x 5 b = 0.0344 0.1869 0.7547 0.1190 0.2238 0.4387 0.4898 0.2760 0.4984 0.7513 0.3816 0.4456 0.6797 0.9597 0.2551 0.7655 0.6463 0.6551 0.3404 0.5060 0.7952 0.7094 0.1626 0.5853 0.6991 And I have an array of zeros and ones with the same dimension: a = 0 1 1 0 0 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 Doing a(logical(b)) gives me the elements I am looking for, but in a vector form: a(logical(b)) ans = 0.4387 0.3816 0.1869 0.4456 0.6463 0.7547 0.2760 0.6551 0.1626 0.4984 0.9597 0.5853 0.2551 0.5060 How can I get the following matrix instead? 0 0.1869 0.7547 0 0 0.4387 0 0.2760 0.4984 0.7513 0.3816 0.4456 ...