Filter löschen
Filter löschen

Loop over combinations of elements of several vectors

6 Ansichten (letzte 30 Tage)
diego solerpolo
diego solerpolo am 18 Okt. 2023
Kommentiert: Dyuman Joshi am 18 Okt. 2023
In the simplest case, if I have vectors , I want to loop over all the possible combinations of their elements, that is, over I wat to do the same thing in a general case where I have and arbitrary number of vectors with respective dimensions , with denoting the k-th component of vector ; that is, I want to loop over all the combinations of elements . How to do this for a general number of vectors? For now, the only thing I came up with is to make nested loops for each vector, but I want some code whose structure does not depend on the number of vectors.
Thanks for your time!

Antworten (2)

Dyuman Joshi
Dyuman Joshi am 18 Okt. 2023
Use ndgrid -
x1 = 1:5;
x2 = [2 3 5 7 11];
x3 = [2 4 6 8 10];
%Store vectors in a cell array
x = {x1,x2,x3};
n = numel(x);
%Preallocate
C = cell(1,n);
%Reverse order to get the proper order when concatenating
[C{end:-1:1}] = ndgrid(x{end:-1:1});
%Concatenate and reshape the data corresponding to number of vectors
out = reshape(cat(n,C{:}),[],n);
disp(out)
1 2 2 1 2 4 1 2 6 1 2 8 1 2 10 1 3 2 1 3 4 1 3 6 1 3 8 1 3 10 1 5 2 1 5 4 1 5 6 1 5 8 1 5 10 1 7 2 1 7 4 1 7 6 1 7 8 1 7 10 1 11 2 1 11 4 1 11 6 1 11 8 1 11 10 2 2 2 2 2 4 2 2 6 2 2 8 2 2 10 2 3 2 2 3 4 2 3 6 2 3 8 2 3 10 2 5 2 2 5 4 2 5 6 2 5 8 2 5 10 2 7 2 2 7 4 2 7 6 2 7 8 2 7 10 2 11 2 2 11 4 2 11 6 2 11 8 2 11 10 3 2 2 3 2 4 3 2 6 3 2 8 3 2 10 3 3 2 3 3 4 3 3 6 3 3 8 3 3 10 3 5 2 3 5 4 3 5 6 3 5 8 3 5 10 3 7 2 3 7 4 3 7 6 3 7 8 3 7 10 3 11 2 3 11 4 3 11 6 3 11 8 3 11 10 4 2 2 4 2 4 4 2 6 4 2 8 4 2 10 4 3 2 4 3 4 4 3 6 4 3 8 4 3 10 4 5 2 4 5 4 4 5 6 4 5 8 4 5 10 4 7 2 4 7 4 4 7 6 4 7 8 4 7 10 4 11 2 4 11 4 4 11 6 4 11 8 4 11 10 5 2 2 5 2 4 5 2 6 5 2 8 5 2 10 5 3 2 5 3 4 5 3 6 5 3 8 5 3 10 5 5 2 5 5 4 5 5 6 5 5 8 5 5 10 5 7 2 5 7 4 5 7 6 5 7 8 5 7 10 5 11 2 5 11 4 5 11 6 5 11 8 5 11 10

Chunru
Chunru am 18 Okt. 2023
You can use combinations.
% arbitrary number of vectors with arbitrary size
v{1}=rand(3,1);
v{2}=rand(2,1);
v{3}=rand(2,1);
p =table2array(combinations(v{:}))
p = 12×3
0.0085 0.6301 0.2202 0.0085 0.6301 0.6508 0.0085 0.7299 0.2202 0.0085 0.7299 0.6508 0.8834 0.6301 0.2202 0.8834 0.6301 0.6508 0.8834 0.7299 0.2202 0.8834 0.7299 0.6508 0.4095 0.6301 0.2202 0.4095 0.6301 0.6508
for i=1:size(p,1) % only one loop instead of nested loop
p(i, :) % this is the combination and do whatever you want in the loop
end
ans = 1×3
0.0085 0.6301 0.2202
ans = 1×3
0.0085 0.6301 0.6508
ans = 1×3
0.0085 0.7299 0.2202
ans = 1×3
0.0085 0.7299 0.6508
ans = 1×3
0.8834 0.6301 0.2202
ans = 1×3
0.8834 0.6301 0.6508
ans = 1×3
0.8834 0.7299 0.2202
ans = 1×3
0.8834 0.7299 0.6508
ans = 1×3
0.4095 0.6301 0.2202
ans = 1×3
0.4095 0.6301 0.6508
ans = 1×3
0.4095 0.7299 0.2202
ans = 1×3
0.4095 0.7299 0.6508

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