필터 지우기
필터 지우기

deleting words from a cell array

조회 수: 5 (최근 30일)
James Connor
James Connor 2015년 11월 11일
편집: Mohammad Abouali 2015년 11월 16일
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)

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 11월 11일
편집: Mohammad Abouali 2015년 11월 11일
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
Mohammad Abouali
Mohammad Abouali 2015년 11월 16일
편집: Mohammad Abouali 2015년 11월 16일
you can also use
x=x(cellfun(@(c) all(c(word==y)==y),x))
i.e. without using find().

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

bio lim
bio lim 2015년 11월 11일
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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by