I'm trying to find a vectorized way to do the following -
X =
0 1
5 5
-1 8
> w=[false true; true false; false true]
w =
0 1
1 0
0 1
I want to index into X's rows using the columns of w, so the first column of w should retrieve row 2 of X, and the second column of w should retrieve rows 1 and 3 of X, and the result would be a single row matrix [5 5], and another matrix [0 1; -1 8], each of which I can then take mean() on to get [5 5], and [-0.5 4.5] (ideally forming a matrix [5 5; -0.5 4.5]).
How can I do this without loops?
My gut feeling is it's possible by converting X into a 3D matrix, but not sure how.
Use
find, which returns the indices of nonzero entries of a given vector. For example,returns [5 5]
Similarly,
X(find(w(:, 2)), :)returnsYou can then proceed with the mean, etc, and eventually stack the resulting matrices vertically like
C = [A; B].