Delete duplicate cell in a cell of complex double.
Show older comments
Hi, how to delete duplicate cell in a cell using Matlab? I can run using single cell, but I can't when I change the input to cell in a cell.
Below is my code:
% Create cell in cell
F = cell(1,3)
F(1,1)={cell(1,1)}
F(1,2)={cell(1,8)}
F(1,3)={cell(1,8)}
F{1,1}(1,1)= {[0.04 0.2 0.56;
0.31 0.67 0.22]}
F{1,2}(1,1)= {[6+6j 7+3j 8-6j;
6+8j 7-6j 3-3j]}
F{1,2}(1,2)= {[5+6j 8+5j 3-6j;
6+8j 7-6j 2+3j]}
F{1,2}(1,3)= {[3-6j 2+1j 8-6j;
5+6j 7+6j 9-3j]}
F{1,2}(1,4)= {[1-1j 7+5j 8+6j;
9+8j 8-6j 9-2j]}
F{1,2}(1,5)= {[6+6j 7+3j 8-6j;
6+8j 7-6j 3-3j]}
F{1,2}(1,6)= {[5+6j 8+5j 3-6j;
6+8j 7-6j 2+3j]}
F{1,2}(1,7)= {[3-6j 2+1j 8-6j;
5+6j 7+6j 9-3j]}
F{1,2}(1,8)= {[1-1j 7+5j 8+6j;
9+8j 8-6j 9-2j]}
F{1,3}(1,1)= {[6+6j 7+3j 8-6j;
6+8j 7-6j 3-3j]}
F{1,3}(1,2)= {[5+6j 8+5j 3-6j;
6+8j 7-6j 2+3j]}
F{1,3}(1,3)= {[3-6j 2+1j 8-6j;
5+6j 7+6j 9-3j]}
F{1,3}(1,4)= {[1-1j 7+5j 8+6j;
9+8j 8-6j 9-2j]}
F{1,3}(1,5)= {[6+6j 7+3j 8-6j;
6+8j 7-6j 3-3j]}
F{1,3}(1,6)= {[5+6j 8+5j 3-6j;
6+8j 7-6j 2+3j]}
F{1,3}(1,7)= {[3-6j 2+1j 8-6j;
5+6j 7+6j 9-3j]}
F{1,3}(1,8)= {[1-1j 7+5j 8+6j;
9+8j 8-6j 9-2j]}
% Delete duplicate cells
for s=1:length(F)
for w=2:length(F{s})
for ii = numel(F{w}):-1:2
for jj = 1:ii-1
if isequal((F{s}{ii}),(F{s}{jj}))
F{s}{ii} = {};
break
end
end
end
end
end
F{:}
The output should be: remain value in F{1}{1}(1,1), F{1,2} (1,1)-(1,4) and F{1,3}(1,1)-(1,4). The rest cell (duplicate cells) will be deleted.
Thank in advance
2 Comments
Stephen23
on 16 Aug 2022
@Noor Huda ja'afar: do you want to remove global duplicates (that occur anywhere in F), or only local duplicates (that only occur in the same cell of F) ?
By the way:
F{1,1} = cell(1,1)
% is more efficient than:
F(1,1) = {cell(1,1)}
In the second case, Matlab creates a cell and inserts it in a cell array. In the first case the array is inserted as cell element directly. The same for:
F{1,1}{1,1} = [0.04 0.2 0.56;
0.31 0.67 0.22]
Does the show code work? I get the error message: "Index exceeds array bounds."
What does "deleted" mean? Do you want empty cells or remove the cells?
Accepted Answer
More Answers (1)
% Create cell in cell
F = cell(1,3);
F{1,1} = {[0.04 0.2 0.56; 0.31 0.67 0.22]};
F{1,2} = {[6+6j 7+3j 8-6j; 6+8j 7-6j 3-3j], ...
[5+6j 8+5j 3-6j; 6+8j 7-6j 2+3j], ...
[3-6j 2+1j 8-6j; 5+6j 7+6j 9-3j], ...
[1-1j 7+5j 8+6j; 9+8j 8-6j 9-2j], ...
[6+6j 7+3j 8-6j; 6+8j 7-6j 3-3j], ...
[5+6j 8+5j 3-6j; 6+8j 7-6j 2+3j], ...
[3-6j 2+1j 8-6j; 5+6j 7+6j 9-3j], ...
[1-1j 7+5j 8+6j; 9+8j 8-6j 9-2j]};
F{1,3} = {[6+6j 7+3j 8-6j; 6+8j 7-6j 3-3j], ...
[5+6j 8+5j 3-6j; 6+8j 7-6j 2+3j], ...
[3-6j 2+1j 8-6j; 5+6j 7+6j 9-3j], ...
[1-1j 7+5j 8+6j; 9+8j 8-6j 9-2j], ...
[6+6j 7+3j 8-6j; 6+8j 7-6j 3-3j], ...
[5+6j 8+5j 3-6j; 6+8j 7-6j 2+3j], ...
[3-6j 2+1j 8-6j; 5+6j 7+6j 9-3j], ...
[1-1j 7+5j 8+6j; 9+8j 8-6j 9-2j]};
% Delete duplicate cells
for iF = 1:numel(F)
G = F{iF}; % Process current cell of F
nG = numel(G);
keep = true(1, nG); % Elements to keep
for iG = 1:nG
for iG2 = iG+1:nG % Check following elements of G
if keep(iG2) % Do not test, if excluded before
keep(iG2) = ~isequal(G{iG}, G{iG2});
end
end
end
F{iF} = G(keep);
end
F{:}
Categories
Find more on Data Type Identification 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!