# 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
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.

mazari ahmed on 14 Mar 2015
Edited: per isakson on 14 Mar 2015
neighbour_n{i}= setdiff(neighbour_n{i},F);
problem solved

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.
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

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)
cellfun(@(x)setdiff(x,8),stated_n,'UniformOutput',false)
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

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.
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

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
mazari ahmed on 9 Apr 2015
problem solved