Delete duplicate cell in a cell of complex double.

조회 수: 6 (최근 30일)
NHJ
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.
Thank in advance
  댓글 수: 2
Stephen23
Stephen23 2022년 8월 16일
@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) ?
Jan
Jan 2022년 8월 16일
편집: 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
Stephen23 2022년 8월 16일
편집: 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
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
Jan 2022년 8월 16일
편집: 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}

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by