Removing certain type of repeating cell

2 vues (au cours des 30 derniers jours)
MatlabQs
MatlabQs le 5 Jan 2022
Commenté : MatlabQs le 6 Jan 2022
Not familiar with coding. I want to remove certain type of repeating cells. I am using this code to find all combinations that equal a given sum:
function C = Conc(N)
P= [1:5 1:5 1:5]
C = {};
for ii = 1:numel(P)
nestfun(P(ii),P(ii+1:end))
end
function nestfun(t,V)
if sum(t)<5
for jj = 1:numel(V)
nestfun([t,V(jj)],V(jj+1:end))
end
elseif sum(t)==5
C{end+1} = t;
end
end
end
This will result in say [1 1 1 2] [1 2 1 1] [1 3 1] [1 3 1] [3 1 1]. How would i remove the repeating [1 3 3]? In this project [1 1 1 2] is different than [1 2 1 1], but [1 3 1] is the same as [1 3 1].
Thank you.

Réponse acceptée

Stephen23
Stephen23 le 6 Jan 2022
Modifié(e) : Stephen23 le 6 Jan 2022
A direct, intuitive, and reasonably efficient approach is to simply check the existing cell array content:
out = Conc(5)
out = 1×14 cell array
{[1 2 1 1]} {[1 2 2]} {[1 3 1]} {[1 4]} {[1 1 2 1]} {[1 1 3]} {[1 1 1 2]} {[2 3]} {[2 1 2]} {[2 2 1]} {[3 1 1]} {[3 2]} {[4 1]} {[5]}
function C = Conc(N)
P = [1:5,1:5,1:5];
C = {};
for ii = 1:numel(P)
nestfun(P(ii),P(ii+1:end))
end
function nestfun(t,V)
if sum(t)<5
for jj = 1:numel(V)
nestfun([t,V(jj)],V(jj+1:end))
end
elseif sum(t)==5
for k = 1:numel(C)
if isequal(C{k},t)
return
end
end
C{end+1} = t;
end
end
end
  1 commentaire
MatlabQs
MatlabQs le 6 Jan 2022
This was very helpful. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by