Separate rows that contain certain string character
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kosta
el 10 de En. de 2017
Comentada: Stephen23
el 10 de En. de 2017
Hi all,
I would like to separate the rows of the cellarray bellow in to two cellarrays, depending on the string character on the third column, whether it ends in 'B' or 'L'.
For example, i have the cellarray:
a={
'STING ONE' 40843 '09L';
'STRING TWO' 40842 '10B';
'STRING THREE' 40841 '11L';
'STRING FOUR' 40840 '12B';
'STRING FIVE' 40839 '03L'}
I would like to split the above automatically into:
b={
'STING ONE' 40843 '09L';
'STRING THREE' 40841 '11L';
'STRING FIVE' 40839 '03L'}
and
c={
'STRING TWO' 40842 '10B';
'STRING FOUR' 40840 '12B'}
would that be possible to do?
Thanks very much in advance for your help!
Kind regards,
Kosta.
0 comentarios
Respuesta aceptada
Stephen23
el 10 de En. de 2017
Editada: Stephen23
el 10 de En. de 2017
>> idx = cellfun(@(s)'B'==s(end),a(:,3));
>> c = a(idx,:)
You could even put this in a function:
>> fun = @(c)a(cellfun(@(s)c==s(end),a(:,3)),:);
>> b = fun('L');
>> c = fun('B');
2 comentarios
Stephen23
el 10 de En. de 2017
A more efficient solution would be to run cellfun once to generate a character array of those characters:
>> vec = cellfun(@(s)s(end),a(:,3))
vec =
L
B
L
B
L
>> b = a(vec=='L',:);
>> c = a(vec=='B',:);
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!