Find row Index of strings in cell Array (Matlab) -


if have cell array c:

c =  {'name'  'hh' '23' []     []     'last'  'bb' '12' '8'    'hello'     'in'    'kk' '12' '2131' []     'name'  'kk' '23' []     []     'name'  'cv' '22' []     []     'name'  'ph' '23' []     [] } ; 

how can row index of rows have 'name' in first column , '23' in third column?

indexresult = [1,4,6]  

the simplest way this (all-versions compatible) use strcmp, can accept cell arrays , "string compare".

one liner

indexresult = find(strcmp(c(:,1), 'name') & strcmp(c(:,3), '23')); % indexresult = [1; 4; 6]; 

explanation

% logical array of rows first column 'name' logicalname = strcmp(c(:,1), 'name'); % logical array of rows third column '23' logical23 = strcmp(c(:,3), '23'); % logical array both of above true, using , (&) logicalname23 = strcmp(c(:,1), 'name') & strcmp(c(:,3), '23'); % indices logical array using find indexresult = find(strcmp(c(:,1), 'name') & strcmp(c(:,3), '23')); % if wanted row vector instead of column vector, transpose indexresult = find(strcmp(c(:,1), 'name') & strcmp(c(:,3), '23')).'; 

if want case insensitive (matching 'name', 'name', 'name', ...) use strcmpi instead of strcmp.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -