MATLAB: Logical indexing of matrix doesn’t output matrix of the correct dimensions

filter rowslogical indexing

I have a matrix 90×22 consisting of some user specific values and timestamp (the last column). After I verified some properties I am now trying to specify the rows of that matrix as follows:
windowLogical = userData(:,end) <= currentTs + 180 & userData(:,end) >= startTs;
windowLogical = repmat(windowLogical, 1, colCnt);
window = userData(windowLogical);
In the first row, I am trying to filter out the rows that meet my criteria. After the first row is executed, the windowLogical variable contains 90×1 logical as follows:
1
1
1
1
1
1
1
1
1
0
0
0
0
... and zeros until row 90
If I now call window = userData(windowLogical), I get the correct matrix in terms of selected rows but the problem here is that the matrix contains only the first column:
1
1
1
1
1
0
0
0
0
(yes, this is the actual content of the first column)
This makes sense since the logical is the only single column. However, I want to select all the columns where the logical is 1. Since I don't know how to force the filtering function to output 90×22 logical, I found a function rempat that replicates the column 22 times, so I actually get 90×22 logical with the correct entries as I want:
90×22 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(.. and so on)
HOWEVER – if I now call window = userData(windowLogical); I get some really weird results. The window is now 198×1 double. !98 is 9 * 22, which corresponds to previously selected 9 rows, 22 cols each. But why do I get this double vector is not quite clear. I would expect to get a matrix 9×22 since the logical is defined as I displayed above.
Could somebody shed some light on this problem and the standard matlab solution?

Best Answer

Is this what you want? I just added the colon : to select all of the columns.
window = userData(windowLogical,:);
(In general, when you use logical indexing as a single index into a matrix, the result will be returned as a column vector ... which is what you were seeing)
Related Question