# delete an element in a cell array in a for loop

23 views (last 30 days)
mazari ahmed on 12 Mar 2015
Commented: mazari ahmed on 9 Apr 2015
hello; l have a set called stated_n{i} where i varies from 1 to n (let n=5) for exemple
stated_n{1} = {3,7,8,9,14,99}
stated_n{2} = {14,8,19,104,98}
stated_n{3} = {67,7,8,9,14,11}
stated_n{4} = {41,76,8,18,14,56}
stated_n{5} = {65,13,16,9,8,103}
l want for exemple to delete a value k (let k= 8) how to do that to remove 8 from each stated_n (1:5)
for i=1:N
if ismember(k,stated_n{i})
%remove k from stated_n{i}
% update stated_n{i} and display it
end
end
##### 3 CommentsShowHide 2 older comments
the cyclist on 12 Mar 2015
OK, but be aware that you keep using curly brackets "{}", which is how cell arrays are specified.
I will modify my code below in a way that I hope will make it work.

Sign in to comment.

### Accepted Answer

mazari ahmed on 14 Mar 2015
Edited: per isakson on 14 Mar 2015
the answer is :
neighbour_n{i}= setdiff(neighbour_n{i},F);
problem solved
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (4)

Sara Hafeez on 12 Mar 2015
The method will be first find the index of the number 8 in a loop in every cell array and then use the following stated_n{index}=[] this will remove the value of 8 and yes find the value ofn8 by comparing or by a conditional statement.
##### 1 CommentShowHide None
mazari ahmed on 12 Mar 2015
Sara look at this exemple let k = 8 (the value of k is dynamic this is why it is mandatory to do this ismember(k,stated_n{i})) % we should verifiy in every stated_n{i} that k is a member if so delete it stated_n{1}= {4,8,9,6} % 8 is a member so delete it stated_n{98}= {6,9,78,87,34} % 8 is not a member so we do anything

Sign in to comment.

the cyclist on 12 Mar 2015
Edited: the cyclist on 12 Mar 2015
If you really have a cell array of cell arrays, then I think this will work:
output = cellfun(@(x)num2cell(setdiff([x{:}],8)),stated_n,'UniformOutput',false)
EDIT BASED ON COMMENTS ABOVE
cellfun(@(x)setdiff(x,8),stated_n,'UniformOutput',false)
##### 4 CommentsShowHide 3 older comments
the cyclist on 12 Mar 2015
Edited: the cyclist on 12 Mar 2015
My code does work.
Use this version:
cellfun(@(x)setdiff(x,8),stated_n,'UniformOutput',false)
It does not need to be inside the for loop:
output = cellfun(@(x)(setdiff(x,F)),neighbour_n,'UniformOutput',false);
for i=1:N
display(['Display new neighbour of ', num2str(i), ' are: ', num2str(neighbour_n{i})]);
end
I defined a new variable called output, but you are just displaying your old variable, neighbour, so you did not see the deletions.
The new variable has what you want. Maybe you want to do
neighbour_n = cellfun(@(x)(setdiff(x,F)),neighbour_n,'UniformOutput',false);
for i=1:N
display(['Display new neighbour of ', num2str(i), ' are: ', num2str(neighbour_n{i})]);
end

Sign in to comment.

Sara Hafeez on 12 Mar 2015
OK don't have a PC here writing this in random on mobile just check if it runs properly For i=1:5% because there are 5 parts
For j=1:6
If stated{i,j}=8 Then set this to an empty array and the end the loop try this hope it works.
##### 1 CommentShowHide None
mazari ahmed on 12 Mar 2015
Edited: mazari ahmed on 12 Mar 2015
but Sara l do't want to delete all the elments of the stated_n{i} but only the element 8

Sign in to comment.

mazari ahmed on 12 Mar 2015
Edited: mazari ahmed on 12 Mar 2015
Even in this way it doesn't work.it doesn't make any changes
N=200;
stated_n=cell(N,1);
for i=1:N
if(ismember(k, stated_n{i}))
ind=num2str(find(ismember(k, neighbour_n{i})));
stated_n{i,ind}= [];
display(num2str(neighbour_n{i});
end
end
##### 1 CommentShowHide None
mazari ahmed on 9 Apr 2015
problem solved

Sign in to comment.

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by