# Delete duplicate cell in a cell of complex double.

5 ビュー (過去 30 日間)
NHJ 2022 年 8 月 16 日
コメント済み: NHJ 2022 年 8 月 16 日
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.
##### 2 件のコメント表示非表示 1 件の古いコメント
Jan 2022 年 8 月 16 日

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?

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

### 採用された回答

Stephen23 2022 年 8 月 16 日

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 件のコメント表示非表示 なし
NHJ 2022 年 8 月 16 日
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

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

### その他の回答 (1 件)

Jan 2022 年 8 月 16 日

% 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}

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

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by