combination and their sum
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
hi can you help me with this
4 2
5 7
3 1
Combination 1: 4,5,3
Combination 2: 4,5,1
Combination 3: 4,7,3
Combination 4:2,5,3
Combination 6: 2,7,1
Combination 7: 2,7,3
Combination 8: 2,5,1
Combination 9: 4,7,1
And sum of each combination
thanks
0 comentarios
Respuestas (2)
the cyclist
el 18 de Oct. de 2019
A = [4 2;
5 7;
3 1];
m = size(A,1);
output = [];
for j = 1:2
output = [output; A(:,j)'];
for i = 1:m
output = [output; [A(1:i-1,j); A(i,3-j); A(i+1:end,j)]'];
end
end
sumOutput = sum(output,2)
This will work for any number of rows, but only for 2 columns.
Bruno Luong
el 19 de Oct. de 2019
Editada: Bruno Luong
el 19 de Oct. de 2019
A = [4 2;
5 7;
3 1]
m = size(A,1);
b = dec2bin(0:2^m-1,m)-'0';
C = A((1:m)+m*b).'
Result (each column is a combination)
C =
4 4 4 4 2 2 2 2
5 5 7 7 5 5 7 7
3 1 3 1 3 1 3 1
Nest you can sum C by
>> sum(C)
ans =
12 10 14 12 10 8 12 10
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!