how to pick up all combination of numbers from multiple vectors
Mostra commenti meno recenti
I have a number of vectors, probably with different lengths, e.g., a=[1 2 3], b=[4 5 6 7] and c=[8 9 10 11 12]. From a, b and c I have 3*4*5=60 possible points, e.g., one possibility is (1,4,8). If I know the number of vectors and the length of each vector in advance, this is easy to program. However, I want to write a general code that can find all these combinations regardless of the number of vectors and their individual lengths
2 Commenti
dpb
il 13 Ago 2019
The problem is using named variables instead cell array or struct with named fields or somesuch.
If do that, you can simply catenate the vectors programmatically with no problem. Of course, you can easily run into memory problems here in that the number grows geometrically.
Mike Croucher
il 4 Apr 2023
As of R2023a, you can use the combinations function. Details at The new combinations function in MATLAB – for cartesian products and parameter sweeps » The MATLAB Blog - MATLAB & Simulink (mathworks.com)
Risposta accettata
Più risposte (3)
Here are two solutions for before/after R2023a.
Starting in R2023a, use the combinations function. Details at The new combinations function in MATLAB – for cartesian products and parameter sweeps » The MATLAB Blog - MATLAB & Simulink
% Demo data
a = 1:3;
b = 11:13;
c = -4:1;
d = 9;
allCombinations = combinations(a,b,c,d)
Before R2023a
This solution places all vectors in a cell array and uses ndgrid to create permutations. See comments for more detail. T
% Use the same demo data created above.
% Put all vectors into cell array
allVecs = {a,b,c,d};
sub = cell(1,numel(allVecs));
[sub{:}] = ndgrid(allVecs{:});
sub = cellfun(@(x)x(:),sub,'UniformOutput', false);
% allPerms is [m x n] matrix of m permutations of n vectors
% m should equal prod(cellfun(@numel,allVecs))
% n should equal numel(allVecs)
allPerms = cell2mat(sub)
Compare results
combinations() produces a table and my second solution produces a matrix with rows in a different order. Here I'll resort the rows of the table to match the rows of the matrix and we'll confirm that the two solutions are equal.
allCombinationsMat = sortrows(allCombinations{:,:},[4 3 2 1]);
isequal(allCombinationsMat, allPerms)
2 Commenti
mohamed Faraj
il 14 Ago 2019
Adam Danz
il 14 Ago 2019
Sounds good. So your variable "x" is my variable "allVecs". Did you have any trouble implementing the rest of the solution?
Bruno Luong
il 13 Ago 2019
Modificato: Bruno Luong
il 13 Ago 2019
a=[1 2 3], b=[4 5 6 7], c=[8 9 10 11 12]
C = {a,b,c}; % put you vectors here
n = length(C);
[C{:}] = ndgrid(C{:});
C = reshape(cat(n+1,C{:}),[],n)
2 Commenti
Mohamad Javadzadeh
il 18 Giu 2021
Hi
Tnx for your great anwser
could we limit sum of rows?
Bruno Luong
il 16 Mar 2022
Modificato: Bruno Luong
il 25 Mar 2024
"could we limit sum of rows?"
Simply post filter what you want to keep (such as limit of sum) afterward.
Chris
il 14 Ago 2019
0 voti
I use allcomb from the Exchange - works great. Works with chars too. It uses ndgrid under the hood and is probably mostly a packaged up version of the code other have shown here.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!