Skip to content

Instantly share code, notes, and snippets.

@lebedov
Last active January 18, 2017 16:26
Show Gist options
  • Save lebedov/b8281d8773f34d64b5e5bc3b0c7ecf66 to your computer and use it in GitHub Desktop.
Save lebedov/b8281d8773f34d64b5e5bc3b0c7ecf66 to your computer and use it in GitHub Desktop.
How to only retain a fixed number of sublists that contain specific values.
% How to only retain a fixed number of sublists that contain specific values.
% Sublists to analyze:
ids = {{0, 0}, {1, 0}, {0, 1, 2}, {0, 2}, {1, 2}, {0, 2}, {1, 1}};
counts = [0, 0, 0];
for i=1:length(ids),
id = cell2mat(ids{i});
% Retain `id` only if it contains at least one
% value not having a count > 3:
retain = 0;
for j=id,
if counts(j+1) < 3,
retain = 1;
break;
end
end
if retain,
fprintf(['id: [', repmat(' %d ', 1, length(id)), ']\n'], id);
fprintf(['counts: [', repmat(' %d ', 1, length(counts)), ']\n'], counts);
fprintf('---\n');
% If j is represented in entry `id`,
% increase the count of retained entries
% that contain j:
for j=[0,1,2],
if ismember(j, id),
counts(j+1) = counts(j+1) + 1;
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment