delete an element in a cell array in a for loop

2 ビュー (過去 30 日間)
mazari ahmed
mazari ahmed 2015 年 3 月 12 日
コメント済み: mazari ahmed 2015 年 4 月 9 日
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 件のコメント
mazari ahmed
mazari ahmed 2015 年 3 月 12 日
no l have just many celle array from 1 to n with different elements : stated_n{1} = {3,7,8,9,14,99} stated_n{2} = {14,8,19,104,98} . . . .
stated_n{200} = {41,76,8,18,14,56,17,999,659,7} stated_n{5} = {65,13,16,9,8,103}
the cyclist
the cyclist 2015 年 3 月 12 日
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
mazari ahmed 2015 年 3 月 14 日
編集済み: per isakson 2015 年 3 月 14 日
the answer is :
neighbour_n{i}= setdiff(neighbour_n{i},F);
problem solved

その他の回答 (4 件)

Sara Hafeez
Sara Hafeez 2015 年 3 月 12 日
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 件のコメント
mazari ahmed
mazari ahmed 2015 年 3 月 12 日
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
the cyclist 2015 年 3 月 12 日
編集済み: the cyclist 2015 年 3 月 12 日
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 件のコメント
mazari ahmed
mazari ahmed 2015 年 3 月 12 日
here is my complete code .l tried it with the second instruction it executes but it doesn't delete the element (cellfun(@(x)setdiff(x,8),stated_n,'UniformOutput',false)) with the first instruction it returns me an error (output = cellfun(@(x)num2cell(setdiff([x{:}],8)),stated_n,'UniformOutput',false)) the error is : ??? Cell contents reference from a non-cell array object.
Error in ==> @(x)num2cell(setdiff([x{:}],F))
Error in ==> said at 96 output = cellfun(@(x)num2cell(setdiff([x{:}],F)),neighbour_n,'UniformOutput',false);
here is my entire code try to execute it :
% code %
X=100;
Y=100;
N=200; %number of nodes
nodesX(1)=rand*X;
nodesY(1)=rand*Y;
for i=2:N
nodesX(i)=rand*X;
nodesY(i)=rand*Y;
d(i-1) =((nodesX(i)-nodesX(i-1)).^2+(nodesY(i)-nodesY(i-1)).^2).^0.5;
while (d(i-1)>200)
nodesX(i)=rand*X;
nodesY(i)=rand*Y;
d(i-1) =((nodesX(i)-nodesX(i-1)).^2+(nodesY(i)-nodesY(i-1)).^2).^0.5;
end
end
h_old=plot(nodesX,nodesY,'m*');
labels=[1:N]';
labels=num2str(labels);
text(nodesX,nodesY,labels);
xlabel('X (Meters)');
ylabel('Y (Meters)');
title(['Network Topology with',num2str(N),'nodes']);
hold on
A= zeros(N,N);
neighbour_n=cell(N,1);
m=0;
for k=1:N;
for j=1:N;
if (k~=j)
d=((nodesX(k)-nodesX(j))^2+(nodesY(k)-nodesY(j))^2)^0.5;
end
if (k~=j);
if(d < 50);
line([nodesX(k),nodesX(j)],[nodesY(k),nodesY(j)]);
neighbour_n{k}= [neighbour_n{k} j];
m=m+1;% the number of created links
A(k, j)=1;
if (A(k, j)==1);
A(j, k)=1;
end
else
A(k, j)=0;
end
end
end;
display(['the neighbours of ', num2str(k), ' are ', ' = ' , num2str(neighbour_n{k})]);
end;
display(A);
display(['the total number of edges is: ', num2str(m)]);
deleted_node=cell(N,1);
for k=1:N;
for j=1:N;
if (((all(ismember(neighbour_n{k}, neighbour_n{j})))||(isequal(neighbour_n{j}, neighbour_n{k})))&&(k~=j));% incorrect syntax
delete(line([nodesX(k),nodesX(j)],[nodesY(k),nodesY(j)]));
display(['the line between ', num2str(k), ' and ', num2str(j), ' is deleted']);
deleted_node{i}= [deleted_node{i} k];
F=Unique(deleted_node{i});
L=length(F);
end
end
end
display(['the deleted nodes are ', ' = ' , num2str(F)]);
display(['the number of deleted nodes is ', ' = ' , num2str(L)]);
eliminated_all=cell(N,1);
candidate_node=cell(N,1);
%N = numel(neighbour_n);
for i=1:N
output = cellfun(@(x)num2cell(setdiff([x{:}],F)),neighbour_n,'UniformOutput',false);
display(['Display new neighbour of ', num2str(i), ' are: ', num2str(neighbour_n{i})]);
end
the cyclist
the cyclist 2015 年 3 月 12 日
編集済み: the cyclist 2015 年 3 月 12 日
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
Sara Hafeez 2015 年 3 月 12 日
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 件のコメント
mazari ahmed
mazari ahmed 2015 年 3 月 12 日
編集済み: mazari ahmed 2015 年 3 月 12 日
but Sara l do't want to delete all the elments of the stated_n{i} but only the element 8

サインインしてコメントする。


mazari ahmed
mazari ahmed 2015 年 3 月 12 日
編集済み: mazari ahmed 2015 年 3 月 12 日
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 件のコメント
mazari ahmed
mazari ahmed 2015 年 4 月 9 日
problem solved

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by