Borrar filtros
Borrar filtros

n-fold Cartesian product

3 visualizaciones (últimos 30 días)
Sundar Aditya
Sundar Aditya el 5 de Mayo de 2016
Comentada: Sundar Aditya el 6 de Mayo de 2016
I need to compute the n-fold Cartesian product of a set, where n is a positive integer determined by some previous code. Is there any way to do this without using nested for-loops? ndgrid seems to be optimal for a fixed n, but I'm unable to find a solution for any general n. Thanks.

Respuesta aceptada

John D'Errico
John D'Errico el 5 de Mayo de 2016
For example:
S = 1:3;
n = 5;
q = cell(1,5);
s = repmat({S},1,n);
[Q{1:n}] = ndgrid(s{:});
Q = reshape(cat(n,Q{:}),[],n);
I'm not sure if that solution is a trivial one. Trivial is all in the eyes of the beholder anyway. To be honest, I've written a function in one of my toolboxes that does it for me, rather than have to remember this trick when I need it.
  1 comentario
Sundar Aditya
Sundar Aditya el 6 de Mayo de 2016
Thanks for the answer, John.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by