Who do I create a sequence of matrices?

5 visualizaciones (últimos 30 días)
SomeUser
SomeUser el 17 de Nov. de 2012
Hello everybody!
I need to create a sequence of matrices of the following form. For example: From the input:
1 2 0
x = 0 0 0
0 0 0
I need to get:
1 0 0 1 1 0 1 2 0 0 1 0 0 2 0
y = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
That is, I need to get a "combinatoric" sequence of matrices from the one input. The size of the matrix can vary. Is there any suitable function for this? Thank you.
  11 comentarios
SomeUser
SomeUser el 17 de Nov. de 2012
Editada: SomeUser el 17 de Nov. de 2012
Yeah, because you specified a 3x3 matrix, this code works for 2x2 matrices only. I noted that.
And I want to extend it for, say, my expample from 1 post, which you just tried to follow.
To reproduse my example for x = [1 2 0;0 0 0;0 0 0]; one should add an additional 5 "for" loops to this code. And that is a problem.
Try to restart my code with x = [1 2; 1 0], for example.
Matt Fig
Matt Fig el 17 de Nov. de 2012
Ah, o.k. Thanks.

Iniciar sesión para comentar.

Respuesta aceptada

Matt Fig
Matt Fig el 17 de Nov. de 2012
Editada: Matt Fig el 18 de Nov. de 2012
This will do the job. Note that all of the matrices are there, but they are in a different order. You can figure out how to re-order them if you work on it. You need this file to make the code work as I have written it: NPERMUTEK.
Note that like most combinatorial problems, things can quickly get out of control with either many elements or a small number of large elements of x.
UPDATE: Simplified Code greatly
function H = combinatoricmat(x)
% help goes here...
L = [size(x) prod(x(x>0)+1)]; % Final array size.
H = npermutek(0:max(x(:)),numel(x));
H = reshape(H(all(bsxfun(@le,H,x(:).'),2),:).',L);
Now test it once it is saved on the MATLAB path:
>> x = [1 2 0;0 0 0;0 0 0];
>> combinatoricmat(x)
ans(:,:,1) =
0 0 0
0 0 0
0 0 0
ans(:,:,2) =
0 1 0
0 0 0
0 0 0
ans(:,:,3) =
0 2 0
0 0 0
0 0 0
ans(:,:,4) =
1 0 0
0 0 0
0 0 0
ans(:,:,5) =
1 1 0
0 0 0
0 0 0
ans(:,:,6) =
1 2 0
0 0 0
0 0 0
  1 comentario
SomeUser
SomeUser el 18 de Nov. de 2012
Thanks alot Matt Fig, works just fine.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 17 de Nov. de 2012
See the "odometer" technique described here

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by