sum all combinations of 2d array
Mostrar comentarios más antiguos
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 comentarios
Alex
el 27 de Oct. de 2011
the cyclist
el 27 de Oct. de 2011
I'd be curious to see the solution you came up with. There's no harm in posting your own answer.
Fangjun Jiang
el 27 de Oct. de 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
el 29 de Nov. de 2017
Editada: Franc Lever
el 29 de Nov. de 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
Respuestas (3)
Walter Roberson
el 27 de Oct. de 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 comentario
Andrei Bobrov
el 28 de Oct. de 2011
values = sum(cat(d+1,combos{:}),d+1);
values = values(:);
Jos (10584)
el 29 de Nov. de 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)
el 29 de Nov. de 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:
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!