Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

# combinations and their sum

1 Ansicht (letzte 30 Tage)
sampath kumar punna am 19 Okt. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
A = [1 2;
1 2;
1 2
1 2];
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
sum=output
sumOutput = sum(output,2)
if you could run this code you can see
this code is missing few combinations like
1 1 2 2
1 2 1 2
1 2 2 1
2 2 1 1
2 1 2 1
2 1 1 2
took 1 and 2 digits to understand combinations clearly.
Is it possible to fix the above error?
Can I get a code for n rows and n columns?

### Antworten (1)

Sulaymon Eshkabilov am 19 Okt. 2019
Hi,
Here is a rather simple answer:
[A1,A2,A3,A4] = ndgrid([1 2 1 2]);
VERS = [A1(:), A2(:), A3(:), A4(:)]
SUM_VERS = sum(VERS, 2)
Good luck.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Diese Frage ist geschlossen.

### Community Treasure Hunt

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

Start Hunting!

Translated by