Search code examples
matlabcell-array

how to keep only the rows greater than 10 in a cell array?


I have a cell array of multiple rows and 2 columns and i want to keep only the rows that have in the second column value greater than 10?

Input:

'OR2V2' 16 
'RAB33B' 1
'ALDOC' 45
'CCNY' 8
'LIM2' 20 
'PECR' 29

Output:

'OR2V2' 16
'ALDOC' 45
'LIM2' 20
'PECR' 29

Solution

  • Input = {'OR2V2' 16 ; 'RAB33B' 1 ; 'ALDOC' 45 ; 'CCNY' 8 ; 'LIM2' 20 ; 'PECR' 29};
    Output = Input([Input{:,2}]>10,:);
    
    %Output = 
    %
    %    'OR2V2'    [16]
    %    'ALDOC'    [45]
    %    'LIM2'     [20]
    %    'PECR'     [29]
    

    [Input{:,2}] gives the matrix containing elements of the second column of Input. You can also use cell2mat(Input(:,2)) instead. Then [Input{:,2}]>10 gives the required logical indices of Input to use for the desired result.