Suppose I have a matrix A and a logical matrix B
A = [1:3 ; 1:3 ; 1:3];B = [1 0 0 ; 0 1 0 ; 0 0 1];B = logical(B);
I want to create a new matrix by indexing matrix A with logical B, so that I can achieve something this
[1 0 0 ; 0 2 0 ; 0 0 3]
However when I use A(B), I only get a vertical array with the three extracted numbers. I want the original 3 by 3 matrix A containing only the integers that correspond to the 1's of matrix B, while assigning the rest of matrix A as zero.
Best Answer