a function with increasing number of its arguments in a loop
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
hi dears,
I'm using a function for generating Cartesian products of two or more vectors and I want the number of its arguments increases in each iteration of a loop (the vectors as its arguments are same). For instance in the first iteration of the loop the function calculates the Cartesian(A,A), in the second iteration calculates Cartesian(A,A,A), in the next iteration calculates Cartesian(A,A,A,A), and so on. How could I do it? Thanks
2 Commenti
Risposte (1)
Stephen23
il 27 Ott 2018
Modificato: Stephen23
il 27 Ott 2018
Use a comma-separated list:
N = ... number of iterations
C = cell(1,N); % output array
for k = 1:N
T = repmat({A},1,1+k);
C{k} = Cartesian(T{:});
end
Read more:
2 Commenti
Stephen23
il 27 Ott 2018
Modificato: Stephen23
il 27 Ott 2018
@Mhedi J: each of the Cartesian products has a different number of rows and columns: this means there is no simple way to concatenate them into one matrix. The simplest solution is to leave them in a cell array, otherwise you would have to pad them with NaN or some other value/s and then concatenate them into one matrix.
Here is a simple example of how to use a cell array:
A = 1:3;
N = 4;
C = cell(1,N);
for k = 1:N;
T = repmat({A},1,1+k);
[T{:}] = ndgrid(T{:});
T = cellfun(@(v)v(:),T,'uni',0);
C{k} = [T{:}];
end
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!