Effacer les filtres
Effacer les filtres

How to delete a duplicate cells?

5 vues (au cours des 30 derniers jours)
NHJ
NHJ le 11 Août 2022
Commenté : NHJ le 11 Août 2022
How to delete a duplicate cells? For example I have 1x4 cells and some cell have the same value. I want to delete the cell, so that my output cell will become smaller.
%Create cell in a cell.
F = {1, 2, 3, 4}
F{1,1} = [3 8;6 4]
F{1,2} = [6 9;7 3]
F{1,3} = [3 8;6 4]
F{1,4} = [6 9;7 3]
%Delete duplicate cells
Z = unique(F)
I get an error because the function unique only can be used with a vector. Suppose the output only have cell F{1,1} and F{1,2}.
How to do that? Thanks in advance

Réponse acceptée

Stephen23
Stephen23 le 11 Août 2022
Modifié(e) : Stephen23 le 11 Août 2022
F = {[3,8;6,4],[6,9;7,3],[3,8;6,4],[6,9;7,3]};
F{:}
ans = 2×2
3 8 6 4
ans = 2×2
6 9 7 3
ans = 2×2
3 8 6 4
ans = 2×2
6 9 7 3
for ii = numel(F):-1:2
for jj = 1:ii-1
if isequal(F{ii},F{jj})
F(ii) = [];
break
end
end
end
F{:}
ans = 2×2
3 8 6 4
ans = 2×2
6 9 7 3
  2 commentaires
NHJ
NHJ le 11 Août 2022
Thank you, this is what I try to do. One more thing, if the value in the cells have a negative sign (negative values), how to ignore the sign? I want it to compare the values only. For example F = {[3,8;6,4],[6,9;7,3],[-3,-8;6,4],[6,-9;7,3]}. Thak you in advance
NHJ
NHJ le 11 Août 2022
I add abs function in the code to have only positive value and I get what I want.
for ii = numel(F):-1:2
for jj = 1:ii-1
if isequal(abs(F{ii}),abs(F{jj}))
F(ii) = [];
break
end
end
end
F{:}

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Software Development Tools dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by