MATLAB: How to filter rows in all slices of a 3D matrix with respect to multiple columns

3d matrixcolumnsfilter

I have a matrix 'weekdata' which is 3476x18x525. In each slice, columns 3:6 are either 0 or 1, and I want to filter out the rows in which columns 3, 4, and 6 = 1 and in which column 5 = 0 for all 525 slices. Doing this for just one was simple enough:
week1=weekdata(:,:,1);
wk1=week1(week1(:,3)==0&week1(:,4)==0&week1(:,5)==1&week1(:,6)==0,:);
Here, output 'wk1' is a 725×18 matrix with only rows that satisfy the above conditions. However, I am not sure how to apply this to each slice without having to do this 525 separate times. On top of this, each slice has a different number of rows that satisfy these conditions (i.e. instead of an output that is 725×18, other slices have outputs with varying rows in the 700's).
Any ideas are appreciated. Thanks.

Best Answer

Use a preallocated cell array to store the output:
N = size(weekdata,3);
C = cell(1,N);
for k = 1:N
idx = weekdata(:,3,k)==0 & weekdata(:,4,k)==0 & weekdata(:,5,k)==1 & weekdata(:,6,k)==0;
C{k} = weekdata(idx,:,k);
end