cols = [1 3 4 6 7];
num_zeros = [3,4];
M_nozeros = cumsum(~M).*~M;
[~,ii] = ismember(M_nozeros(:,cols),num_zeros);
[k,jj] = ndgrid(1:size(M,1),cols);
idx = [k(:),jj(:)];
out = accumarray(ii(:) + 1,1:numel(ii),[],@(x){idx(x,:)});
out = out(2:end);
out - cell array, here each cell corresponds to the ordinal number "0" ( num_zeros), each cell contains a double matrix: rows are the ordinal number of the row containing zero, the columns correspond to cols.
Use:
>> cols = [1 3 4 6 7];
num_zeros = [3,4];
M = rand(15,10) < .25
M =
15×10 logical array
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 1
1 0 0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0
0 0 0 1 1 1 1 1 0 1
0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 1 1 0 0 0
1 0 0 0 0 0 1 0 0 0
>>
>> M_nozeros = cumsum(~M).*~M;
[~,ii] = ismember(M_nozeros(:,cols),num_zeros);
[k,jj] = ndgrid(1:size(M,1),cols);
idx = [k(:),jj(:)];
out = accumarray(ii(:) + 1,1:numel(ii),[],@(x){idx(x,:)});
out = out(2:end);
>>
>> out{:}
ans =
4 1
3 3
3 6
5 4
5 7
ans =
4 6
5 1
6 7
4 3
7 4
>>
Best Answer