This will give you what you want:
>> data = [1 2 3; 4 5 6]; >> mask = [true false true]; >> data(:,mask) ans = 1 3 4 6
This works because you can simply apply the logical mask index to the columns by selecting all rows with :
Even if a 2-D logical array is used for input, the output will be an array of columns with indexed values. This is because there is no guarantee that indexed elements can be organized into a two-dimensional (i.e., rectangular) output. See if your two-dimensional mask was as follows:
mask = [true false true; true false false];
This will index 3 values ββthat cannot be organized into anything other than a row or column for output. Here is another example:
mask = [true true true; true false false];
This will index 4 values, but 3 from the first row, and 1 from the second row. How should these values ββbe formed into a rectangular output matrix? Since there is no clear way to do this in general for an arbitrary two-dimensional index matrix, the column vector with indexed values ββis returned.
source share