-
Notifications
You must be signed in to change notification settings - Fork 1
/
cellsubselect.m
60 lines (51 loc) · 1.54 KB
/
cellsubselect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function [y] = cellsubselect(x, boolvec, dim)
% [Y] = CELLSUBSELECT(X, BOOLVEC, DIM) outputs a cell-arry Y with the same dimension as X
% but from each input cell a subset of rows or columns are kept according
% to boolvec (which assumes the input data to be concatenated across cells)
%
% X should be a linear cell-array of matrices for which the size in at
% least one of the dimensions should be the same for all cells and the sum of samples in
% the other dimension should equal the length of boolvec
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if nargin<3
% reconstruct dim from the boolvec
if length(boolvec)==sum(scx1),
dim = 1;
elseif length(boolvec)==sum(scx2),
dim = 2;
elseif all(scx1==scx1(1)) && numel(boolvec==scx1(1))
dim = 1;
elseif all(scx2==scx2(1)) && numel(boolvec==scx2(1))
dim = 2;
else
error('the length of boolvec should correspond to the summed number of samples across on of the dimensions of the input cells');
end
end
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1),
error('incorrect input for cellsubselect');
end
switch dim
case 1
nsmp = scx1;
case 2
nsmp = scx2;
otherwise
error('dim argument should be 1 or 2');
end
if numel(boolvec)==sum(nsmp)
sel = mat2cell(boolvec,1,nsmp(:)');
elseif numel(boolvec)==nsmp(1)
sel = repmat({boolvec}, nx);
end
dim = repmat({dim}, nx);
y = cellfun(@subc, x, sel, dim, 'UniformOutput', 0);
function [y] = subc(x, boolvec, dim)
switch dim
case 1
y = x(boolvec, :);
case 2
y = x(:, boolvec);
otherwise
end