Remove row in cell array depending on value of first column.

7 views (last 30 days)
I would like to know how to remove each row on each cell in a cell array where the value of the first column is below 6.
I’ve got this cell array with hundreds of cells where each cell is a matrix of a different dimension. The fist 8 cells look like:
C = {36x4 double}
{5x18 double}
{5x2 double}
{13x9 double}
{2x7 double}
{20x2 double}
{20x2 double}
{9x7 double}
For example the third cell of C is {5x2 double} and is composed of:
1 5
19 10
5 4
6 6
21 9
So the end result for that cell should be:
19 10
6 6
21 9

Accepted Answer

Matt J
Matt J on 22 Aug 2015
Edited: Matt J on 22 Aug 2015
result = cellfun(@(z) z(z(:,1)>=6,:) , C ,'uni',0)

More Answers (2)

Cedric
Cedric on 22 Aug 2015
Edited: Cedric on 22 Aug 2015
The easiest way is using a loop:
for cId = 1 : numel( C )
isLt6 = C{cId}(:,1) < 6 ;
C{cId} = C{cId}(~isLt6,:) ; % Or C{cId}(isLt6,:) = [] ;
end
Then you have more compact ways to do it, which may not be more efficient overall, e.g.:
C = cellfun( @(c) c(c(:,1)>=6,:), C, 'UniformOutput', false ) ;

the cyclist
the cyclist on 22 Aug 2015
Edited: the cyclist on 22 Aug 2015
output = cellfun(@(x)x(x(:,1) >= 6,:),C,'UniformOutput',false)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!