I want to remove an entry in a cell array if it is equal to something specifically. Explained in this example:
animals={'cat', 'dog', 'mouse', 'horse'};
"animals" is being redefined in a loop, and if it happens to be redefined with 'dog' as an entry, I want to remove it. So I want
animals={'cat', 'mouse', 'horse'};
I don't want to replace 'dog' with a blank (''), I want to remove that entry entirely and keep the array tight (i.e. reduce the dimensions from 4x1 to 3x1 in this case).
Thanks

 채택된 답변

James Tursa
James Tursa 2018년 1월 31일

7 개 추천

animals(ismember(animals,'dog')) = [];

댓글 수: 2

Nischal Amin
Nischal Amin 2022년 10월 10일
This does not work for me.
X = ('Cat', 'Dog', 'Tiger');
X = X(ismember(X,'Tiger')) == []
X should be 'Cat', 'Dog', Empty cell
Basically, I don't want the X index to change. I just want to remove Tiger. So, when I do unique(X)...it displays only Cat and Dog.
X = ('Cat', 'Dog', 'Tiger') is no valid Matlab syntax. For a cell array you need curly braces, not parentheses.
X(ismember(X,'Tiger')) == [] compares the cell {'Tiger'} with the empty matrix. It does not set anything to the empty cell.
X = {'Cat', 'Dog', 'Tiger'};
X(ismember(X,'Tiger')) = {''}
X = 1×3 cell array
{'Cat'} {'Dog'} {0×0 char}
% Or faster:
X = {'Cat', 'Dog', 'Tiger'};
X(strcmp(X, 'Tiger')) = {''}
X = 1×3 cell array
{'Cat'} {'Dog'} {0×0 char}

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

추가 답변 (2개)

Akira Agata
Akira Agata 2018년 1월 31일

11 개 추천

Another way to do it:
idx = strcmp(animals,'dog');
animals(idx) = [];

댓글 수: 1

Jan
Jan 2018년 1월 31일
If only one string has to be removed, this is the fastest solution. +1

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

Jan
Jan 2018년 1월 31일

6 개 추천

animals = {'cat', 'dog', 'mouse', 'horse'};
animals = setdiff(animals, {'dog'})
This would allow to remove multiple elements at once.

댓글 수: 7

I tried with your command in the following code:
code:
C = partitions([5 7 8 50])
partitions = setdiff(partitions, {'5','7'}).But i am getting error.
could you please help me to remove {'5','7'}.
Jan
Jan 2018년 10월 31일
Please post the error message instead of only mentioning, that there is one. By the way, which partitions do you mean? This is not a function of the Matlab toolboxes, so we cannot know, what you are talking of. At least it looks strange, that the input of this function is numeric, and you expect the output to be a cell string.
Peter Jarosi
Peter Jarosi 2019년 7월 10일
편집: Peter Jarosi 2019년 7월 10일
setdiff() unexpectedly sorts cells. What if I dont want this side effect?
animals = {'cat', 'dog', 'mouse', 'horse'};
animals = setdiff(animals, {'dog'})
animals =
'cat' 'horse' 'mouse'
OK, I found the answer, using 'stable' option. Nice! I really appreciate your idea. Thanks!
animals = {'cat', 'dog', 'mouse', 'horse'};
animals = setdiff(animals, {'dog'}, 'stable')
animals =
'cat' 'mouse' 'horse'
@Peter: If you want to remove one string only:
animals = {'cat', 'dog', 'mouse', 'horse'};
animals = animals(~strcmp(animals, 'dog'))
Peter Jarosi
Peter Jarosi 2019년 7월 11일
@Jan: Actually I applied your idea for removing more than one string from a cell array. (I just used your example to illustrate the problem of sorting.) In my opinion the most convenient method is yours extending with 'stable' option, if we want to remove more than one strings and keep the original order of the remaining elements.
RITESH KUMAR
RITESH KUMAR 2019년 9월 18일
can we write element number at place of dog. it is at 2nd place. can i write 2.
Arpit Agarwal
Arpit Agarwal 2020년 1월 9일
cool

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2018년 1월 31일

댓글:

Jan
2022년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by