deleting words from a cell array

1 visualización (últimos 30 días)
James Connor
James Connor el 11 de Nov. de 2015
Editada: Mohammad Abouali el 16 de Nov. de 2015
if I have a cell array
x={'areach';'aread';'areal';'areality';'arean';'arear';'areasoner';'areaway';'areca';'arecaceae'}
y='a'
word='areach'
How do I delete all the words from x that do not have y in the positions it appears in word
for example 'a' appears as the 1st and 4th letter of word, delete all words from dictionary that do not have 'a' as their 1st and 4th position letters. so x would become
x={'areach';'aread';'areal';'areality';'arean';'arear';'areasoner';'areaway'}
(it got rid of 'areca' and 'arecaceae)

Respuesta aceptada

Mohammad Abouali
Mohammad Abouali el 11 de Nov. de 2015
Editada: Mohammad Abouali el 11 de Nov. de 2015
x={'areach';'aread';'areal'; ...
'areality';'arean';'arear'; ...
'areasoner';'areaway';'areca'; ...
'arecaceae'};
y='a';
word='areach';
% This line does the trick;
x=x(cellfun(@(c) all(c(find(word==y))==y),x))
x =
'areach'
'aread'
'areal'
'areality'
'arean'
'arear'
'areasoner'
'areaway'
Note that the position 1 and 4 are not hard coded. They are defined based on the y and word
  1 comentario
Mohammad Abouali
Mohammad Abouali el 16 de Nov. de 2015
Editada: Mohammad Abouali el 16 de Nov. de 2015
you can also use
x=x(cellfun(@(c) all(c(word==y)==y),x))
i.e. without using find().

Iniciar sesión para comentar.

Más respuestas (1)

bio lim
bio lim el 11 de Nov. de 2015
x={'areach';'aread';'areal';'areality';'arean';'arear';'areasoner';'areaway';'areca';'arecaceae'};
for n = 1 : length(x)
if ~strcmp(x{n}(4),'a')
x{n}=[];
end
end

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!

Translated by