Sum all possible combinations of tablecolumns

3 vues (au cours des 30 derniers jours)
Senne Van Minnebruggen
Senne Van Minnebruggen le 23 Mai 2020
Modifié(e) : KSSV le 23 Mai 2020
I have a table with 30 columns. Now i would like create new tables for all possible summations of the table columns.
Like :
  • a table for all possible summations of 2 columns
  • a table for all possible summations of 3 colomns
  • a table for all possible summations of 4 colomns
  • ...
Is there a way/ matlab function of combination of functions to realise this?

Réponse acceptée

KSSV
KSSV le 23 Mai 2020
You can get all possible combinations of k columns choosen out of given n columns using nchoosek. REad about this function. You can get the sum of columns using sum. I have give a demo of calculating sum of two coumns with all possible combinations out of give columns. This can bve extended to any of your interested case.
A = rand(10,5) ; % matrix for demo
[m,n] = size(A) ; % get size of matrix
% Get possilbe combinations out of 5 columns when 2 columns are pciked
col = 1:n ;
idx = nchoosek(col,2) ;
% Add all combinations of two columns pciked from A
iwant = zeros(m,size(idx,1)) ; % initialize the required
for i = 1:size(idx,1)
iwant(:,i) = sum(A(:,idx(i,:)),2) ;
end
  2 commentaires
Senne Van Minnebruggen
Senne Van Minnebruggen le 23 Mai 2020
This method works only when the number of combination is small. In my case i have a table with 30 columns. So when i would like all the possible summations of 15 columns there are (nchooske(30,15)) 155117520 possible combinations. This is causing the following errors:
Error using zeros
Requested 155117520x15 (17.3GB) array exceeds maximum array size preference. Creation of arrays greater than
this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference
panel for more information.
Error in nchoosek>combs (line 164)
P = zeros(total, k, 'like', v);
Error in nchoosek (line 123)
c = combs(v,k);
KSSV
KSSV le 23 Mai 2020
Modifié(e) : KSSV le 23 Mai 2020
Yes it needs a huge memory.....try to run it on a pc with good RAM. Or if you have GPU card try to explore that option. It turns out to be an array of size 155117520x15. I could run it in my 4 GB machine.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by