Who do I create a sequence of matrices?

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 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 17 Nov. 2012
What is the relation y=f(x)
Walter Roberson
Walter Roberson am 17 Nov. 2012
Bearbeitet: Walter Roberson am 17 Nov. 2012
Why are multiple 1 allowed? And not multiple 2 ? Why is the first matrix different from the fourth and yet none of the matrices show a 2 in the first column ?
SomeUser
SomeUser am 17 Nov. 2012
Because the element y(2,2) can only be 0,1,2. Generaly, y(i,j) can lie between 0 and x(i,j). Only integers allowed.
Azzi Abdelmalek
Azzi Abdelmalek am 17 Nov. 2012
Bearbeitet: Azzi Abdelmalek am 17 Nov. 2012
it's still not clear for me. How x and y are related?
SomeUser
SomeUser am 17 Nov. 2012
x if a matrix. y is a 3D array, where y(i,j,:) are matrices like in my first post. The size(y,3) is a number of all such matrices.
the cyclist
the cyclist am 17 Nov. 2012
What people are saying here is that we do not understand the rule for generating y from x. Please give more than a one-sentence explanation. Is your example y ALL the values that you would expect from that x? Please trust us when we say it is simply not at all clear what the output should be for general x.
Matt Fig
Matt Fig am 17 Nov. 2012
Bearbeitet: Matt Fig am 17 Nov. 2012
The rule seems to be:
For each non-zero value in x, generate a set of matrices where that value is held fixed while all other values vary from 0 to the value. So you can see there is a 1 and a 2. So hold the 1 fixed and generate matrices by letting the element in the position of the 2 vary from 0 to 2. Then hold the 2 fixed and generate matrices by letting the element in the 1 position vary from 0 to 1.
Or something like that....
So there is a typo in y??
Yeah, something like that, but i'll better show it. For 2x2 matrix the result should be:
y = zeros(size(x));
for i = 0:x(1,1)
for j = 0:x(1,2)
for k = 0:x(2,1)
for s = 0:x(2,2)
matrix = [[i;k],[j;s]];
y = cat(3, y, matrix);
end
end
end
end
But I need to do it without "fors" cause this should work for matrix of any dimension.
SomeUser, that code produces an error with the x you show....
x = [1 2 0;0 0 0;0 0 0];
Error using cat
CAT arguments dimensions are not consistent.
Error in for_loop3 (line 13)
y = cat(3, y, matrix);
SomeUser
SomeUser am 17 Nov. 2012
Bearbeitet: SomeUser am 17 Nov. 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 am 17 Nov. 2012
Ah, o.k. Thanks.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt Fig
Matt Fig am 17 Nov. 2012
Bearbeitet: Matt Fig am 18 Nov. 2012

0 Stimmen

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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 17 Nov. 2012

0 Stimmen

See the "odometer" technique described here

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by