How to find sum of row vector and check this is equal to 0?

1 次查看(过去 30 天)
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".

回答(2 个)

David Hill
David Hill 2022-3-1
编辑:David Hill 2022-3-1
idx=sum(D==0,2)<=1&sum(D,2)==0;
d=D(idx,:);
e=E(idx,:);

Walter Roberson
Walter Roberson 2022-3-1
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,:)
ans = 288×6
-1 -4 1 -1 4 1 1 -4 -1 -1 4 1 -1 2 -5 5 -2 1 -5 2 -1 5 -2 1 -1 1 -5 5 -2 2 -1 1 1 -1 4 -4 1 1 -1 -1 4 -4 -5 1 -1 5 -2 2 -2 -3 -1 1 3 2 -1 -3 -2 1 3 2
That sort of thing ??
  4 个评论
bala maths
bala maths 2022-3-3
编辑:bala maths 2022-3-3
1 -4 1 -1 4 1 (here 1 is repeated) remove it
1 -4 -1 -1 4 1 (Here 1 & -1 are repeated) remove it
-1 2 -5 5 -2 1 (all entry are distinct) i want print this type of rows only
-5 2 -1 5 -2 1 (all entry are distinct) i want print this type of rows only
-1 1 -5 5 -2 2
-1 1 1 -1 4 -4
1 1 -1 -1 4 -4
-5 1 -1 5 -2 2 (all entry are distinct) i want print this type of rows only
-2 -3 -1 1 3 2 (all entry are distinct) i want print this type of rows only
-1 -3 -2 1 3 2 (all entry are distinct) i want print this type of rows only
Walter Roberson
Walter Roberson 2022-3-3
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,:)
ans = 216×6
-1 2 -5 5 -2 1 -5 2 -1 5 -2 1 -1 1 -5 5 -2 2 -5 1 -1 5 -2 2 -2 -3 -1 1 3 2 -1 -3 -2 1 3 2 -2 2 -5 5 -1 1 -2 2 -1 1 3 -3 -1 2 -2 1 3 -3 -5 2 -2 5 -1 1
E(mask,:)
ans = 216×6
-1 -2 3 -3 2 1 -1 -2 3 1 2 -3 -1 -2 2 -3 3 1 -1 -2 2 1 3 -3 -1 -3 -2 2 3 1 -1 -3 -2 1 3 2 -1 -3 3 -2 2 1 -1 -3 3 2 -2 1 -1 -3 3 1 -2 2 -1 -3 3 1 2 -2

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by