sum all combinations of 2d array
Show older comments
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 Comments
Alex
on 27 Oct 2011
the cyclist
on 27 Oct 2011
I'd be curious to see the solution you came up with. There's no harm in posting your own answer.
Fangjun Jiang
on 27 Oct 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
on 29 Nov 2017
Edited: Franc Lever
on 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
Answers (3)
Walter Roberson
on 27 Oct 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 Comment
Andrei Bobrov
on 28 Oct 2011
values = sum(cat(d+1,combos{:}),d+1);
values = values(:);
Jos (10584)
on 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
The function PERMN can be downloaded here: https://uk.mathworks.com/matlabcentral/fileexchange/7147-permn-v--n--k-
Jos (10584)
on 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:
Categories
Find more on Creating and Concatenating Matrices 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!