sum all combinations of 2d array

7 Ansichten (letzte 30 Tage)
Alex
Alex am 27 Okt. 2011
Beantwortet: Jos (10584) am 29 Nov. 2017
So I have an array
a = [1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16];
now i want to sum every combination. Like a number padlock, i.e
value1 = a(1,1)+a(2,1)+a(3,1)+a(4,1);
value2 = a(1,1)+a(2,1)+a(3,1)+a(4,2);
value3 = a(1,1)+a(2,1)+a(3,1)+a(4,3);
value4 = a(1,1)+a(2,1)+a(3,1)+a(4,4);
value5 = a(1,1)+a(2,1)+a(3,2)+a(4,1);
value6 = a(1,1)+a(2,1)+a(3,2)+a(4,2);
value7 = a(1,1)+a(2,1)+a(3,2)+a(4,3);
value8 = a(1,1)+a(2,1)+a(3,2)+a(4,4);
value9 = a(1,1)+a(2,1)+a(3,3)+a(4,1);
value10 = a(1,1)+a(2,1)+a(3,3)+a(4,2);
value11 = a(1,1)+a(2,1)+a(3,3)+a(4,3);
value12 = a(1,1)+a(2,1)+a(3,3)+a(4,4);
etc etc etc
except I cant figure out the for loops. Espscially with the fact that the array size can change, so I cant just statically place for loops inside of each other, because i dont necessarily know how many rows/columns there will be? Has anyone done anything like this? Thanks
  4 Kommentare
Fangjun Jiang
Fangjun Jiang am 27 Okt. 2011
Are these all the combination or just a sample of examples? What is the logic for picking the numbers? one number from each row (or column)?
Franc Lever
Franc Lever am 29 Nov. 2017
Bearbeitet: Franc Lever am 29 Nov. 2017
Like This, and in C you cna input any operation or function you wish
a=[1 2 3 4 5];
b=[1 2 3 5 6];
[A,B]=meshgrid(a,b);
C=A+B
C =
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
6 7 8 9 10
7 8 9 10 11

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 27 Okt. 2011
by_row = mat2cell(a,ones(1,size(a,1)),size(a,2));
d = numel(by_row);
combos = cell(1,d);
[combos{:}] = ndgrid(by_row{:});
values = sum(cat(d+1,combos{:}),d+1);
The result of this will be that values will be an array, each dimension of which is the number of columns in a, with the number of dimensions being the same as the number of rows in a.
For example, a=rand(3,4) would give a 4 x 4 x 4 result.
(I'll have to recheck whether this is the desirable output size when I have a bit more time.)
  1 Kommentar
Andrei Bobrov
Andrei Bobrov am 28 Okt. 2011
values = sum(cat(d+1,combos{:}),d+1);
values = values(:);

Melden Sie sich an, um zu kommentieren.


Jos (10584)
Jos (10584) am 29 Nov. 2017
a = [1,2,3,4; 5,6,7,8; 9,10,11,12; 13,14,15,16]; % data
[n,m] = size(a) ;
c = permn(1:m, n) ; % get all permutations of the column indices
r = repmat(1:n, size(c,1), 1) ; % create matching row indices
i = sub2ind([n m], r) ; % transform row and column subindices into linear indices
values = sum(a(i), 2) ; % sum rows to get the required values

Jos (10584)
Jos (10584) am 29 Nov. 2017
And another, faster, solution:
a = [1,2,3,4; 5,6,7,8; 9,10,11,12; 13,14,15,16]; % data
c = mat2cell(a,size(a,1),ones(1,size(a,2)))
values = sum(allcomb(c{:}),2) ;
The function ALLCOMB can be downloaded from the File Exchange:

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by