Finding all possible row combinations of a matrix that add to zero
Afficher commentaires plus anciens
Hello,
I'm looking for a general way to find all possible row combinations of a matrix that add to zero.
For instance, for the matrix
A = [-1 0 0 ; 1 0 0 ; 1 -1 0 ; 0 1 -1 ; 0 1 -1; 0 0 1];
the following 6 row combinations would all sum to zero
A(1,:)+A(2,:)
A(1,:)+A(3,:)+A(4,:)+A(6,:)
A(1,:)+A(3,:)+A(5,:)+A(6,:)
-A(2,:)+A(3,:)+A(4,:)+A(6,:)
-A(2,:)+A(3,:)+A(5,:)+A(6,:)
-A(4,:)+A(5,:)
Does MATLAB have any built-in functions that can help me do this? Generating all possible row combinations and testing to see which ones sum to zero seems like it would be extremely computationally intensive.
Thanks,
Kevin
Réponse acceptée
Plus de réponses (4)
Azzi Abdelmalek
le 21 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 21 Fév 2013
Ok try this
Edit
A = [-1 0 0 ;1 0 0; 1 -1 0 ; 0 1 -1 ; 0 1 -1; 0 0 1];
n=size(A,1)
idx1=npermutek([0 -1 1],n);
p=size(idx1,1);
out=cell(p,1);
for k=1:p
out{k}=sum(bsxfun(@times,A,idx1(k,:)'),1);
end
M=cell2mat(out)
find(~any(M,2))
10 commentaires
Kevin Bachovchin
le 21 Fév 2013
Azzi Abdelmalek
le 21 Fév 2013
Are you sur of your result?
Azzi Abdelmalek
le 21 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 21 Fév 2013
Look at the different combinaisons that give 0;
idx1(find(~any(M,2)),:)
each row corresponds to one combinaison
Kevin Bachovchin
le 21 Fév 2013
Modifié(e) : Kevin Bachovchin
le 21 Fév 2013
Azzi Abdelmalek
le 21 Fév 2013
There is just one individual which is equal to zero, the first one.
Kevin Bachovchin
le 21 Fév 2013
Azzi Abdelmalek
le 21 Fév 2013
Yes I know, what do you mean?
Kevin Bachovchin
le 21 Fév 2013
Azzi Abdelmalek
le 21 Fév 2013
Ok, You did not specify that in your question. You should give all information in your question to avoid wasting of time.
Kevin Bachovchin
le 21 Fév 2013
Jan
le 21 Fév 2013
0 votes
Azzi Abdelmalek
le 21 Fév 2013
Try this
A = [-1 0 0 ;1 0 0; 1 -1 0 ; 0 1 -1 ; 0 1 -1; 0 0 1];
n=size(A,1)
idx1=npermutek([0 -1 1],n);
p=size(idx1,1);
out=cell(p,1);
for k=1:p
out{k}=sum(bsxfun(@times,A,idx1(k,:)'),1);
end
M=cell2mat(out)
ii=find(~any(M,2))
idx2=idx1(ii,:)
for k=1:size(idx2,1)
jj{k}=find(idx2(k,:))
end
for k=1:numel(jj)
iddx{k}=find(cellfun(@(x) isequal(x,jj{k}),jj))
ee(k)=iddx{k}(1)
end
result=idx2(unique(ee),:)
1 commentaire
Kevin Bachovchin
le 21 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
Try this code
A = [-1 0 0 ;1 0 0; 1 -1 0 ; 0 1 -1 ; 0 1 -1; 0 0 1];
n=size(A,1)
idx1=npermutek([0 -1 1],n);
p=size(idx1,1);
out=cell(p,1);
for k=1:p
out{k}=sum(bsxfun(@times,A,idx1(k,:)'),1);
end
M=cell2mat(out);
ii=find(~any(M,2));
idx2=idx1(ii,:);
for k=1:size(idx2,1);
jj{k}=find(idx2(k,:));
end
for k=1:numel(jj);
iddx{k}=find(cellfun(@(x) isequal(x,jj{k}),jj));
ee(k)=iddx{k}(1);
end
result=idx2(unique(ee),:);
n=size(result,1);
test=0;
ii=1;
while test==0
ii=ii+1;
a=find(result(ii,:));
c=result(ii,a);
e=result(:,a);
f=find(ismember(e,c,'rows'));
f(1)=[];
if ~isempty(f);
result(f,:)=[];
n=n-1;
end
if ii==n-1
test=1;
end
end
result
1 commentaire
Kevin Bachovchin
le 25 Fév 2013
Catégories
En savoir plus sur Linear Algebra dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!