How to find sum of row vector and check this is equal to 0?
6 views (last 30 days)
Show older comments
A = [0 1 0 0 0 1 ; 1 0 1 0 0 0 ; 0 1 0 1 0 0 ; 0 0 1 0 1 0; 0 0 0 1 0 1 ; 1 0 0 0 1 0];
B = [1; 2; 3; -3; -2; -1];
C = perms (B);
E=C
D=C*A
output:
E = 3 2 1 -1 -2 -3
3 2 1 -1 -3 -2
3 2 1 -2 -1 -3
3 2 1 -2 -3 -1
3 2 1 -3 -1 -2
3 2 1 -3 -2 -1 and so on...
D = -1 4 1 -1 -4 1
0 4 1 -2 -3 0
-1 4 0 0 -5 2
1 4 0 -2 -3 0
0 4 -1 0 -5 2 and so on...
Here i have lot of outputs for E and D.
so, I want print only the resultant Matrix D with two conditions
i) Matrix D has atmost one zero.
ii) Sum of all elements in D = 0.
and also their corresponding combination of Matrix E
by using any conditional statements (example for , if conditions).
- if exist such matrix D & E, then display "Matrix D and corresponding Matrix E"
- if does not exist such Matrix, then display " there is no such combination".
0 Comments
Answers (2)
David Hill
on 1 Mar 2022
Edited: David Hill
on 1 Mar 2022
idx=sum(D==0,2)<=1&sum(D,2)==0;
d=D(idx,:);
e=E(idx,:);
1 Comment
David Hill
on 1 Mar 2022
h=histc(D,unique(D),2);
idx=sum(D==0,2)<=1&sum(D,2)==0&~sum(h>1,2)>0;
d=D(idx,:);
e=E(idx,:);
Walter Roberson
on 1 Mar 2022
A = [0 1 0 0 0 1 ; 1 0 1 0 0 0 ; 0 1 0 1 0 0 ; 0 0 1 0 1 0; 0 0 0 1 0 1 ; 1 0 0 0 1 0];
B = [1; 2; 3; -3; -2; -1];
C = perms (B);
E=C;
D=C*A;
mask = sum(D==0,2) <= 1 & sum(D,2) == 0;
D(mask,:)
That sort of thing ??
4 Comments
Walter Roberson
on 3 Mar 2022
A = [0 1 0 0 0 1 ; 1 0 1 0 0 0 ; 0 1 0 1 0 0 ; 0 0 1 0 1 0; 0 0 0 1 0 1 ; 1 0 0 0 1 0];
B = [1; 2; 3; -3; -2; -1];
C = perms (B);
E=C;
D=C*A;
[~, F] = mode(D, 2);
mask = sum(D==0,2) <= 1 & sum(D,2) == 0 & F == 1;
D(mask,:)
E(mask,:)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!