Delete duplicate cell in a cell of complex double.

2 views (last 30 days)
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
Jan
Jan on 16 Aug 2022
Edited: Jan on 16 Aug 2022
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?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Aug 2022
Edited: Stephen23 on 16 Aug 2022
As far as I can tell, you only want to remove local duplicate (i.e. only checking within the same cell of F):
F = {{[0.04,0.2,0.56;0.31,0.67,0.22]},{...
[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]},{...
[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]}} % more efficient way to create that array
F = 1×3 cell array
{1×1 cell} {1×8 cell} {1×8 cell}
F{:}
ans = 1×1 cell array
{2×3 double}
ans = 1×8 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double}
ans = 1×8 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double} {2×3 double}
for kk = 1:numel(F)
for ii = numel(F{kk}):-1:2
for jj = 1:ii-1
if isequal((F{kk}{ii}),(F{kk}{jj}))
F{kk}(ii) = [];
break
end
end
end
end
F{:}
ans = 1×1 cell array
{2×3 double}
ans = 1×4 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double}
ans = 1×4 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double}
Note that this is the same as my other answer, just replacing F with F{kk} and the corresponding outer loop:
  1 Comment
NHJ
NHJ on 16 Aug 2022
Yes, this is what I want. Remove the local duplicate. I was quite confused when it became a cell in a cell. By the way, thank you very much

Sign in to comment.

More Answers (1)

Jan
Jan on 16 Aug 2022
Edited: Jan on 16 Aug 2022
% 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{:}
ans = 1×1 cell array
{2×3 double}
ans = 1×4 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double}
ans = 1×4 cell array
{2×3 double} {2×3 double} {2×3 double} {2×3 double}

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!