Home > Archive > Matlab > April 2005 > Combining values into a matrix
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Combining values into a matrix
|
|
|
| I have a problem that seems to me like it should be easy, but I can't
get it to go. Suppose I have the following data:
a = [1 2 3]
b = [45 60]
c = [100 200]
What is the most efficient way to build a matrix such that each row
is a unique combination of values from each variable? An important
feature must be that it should handle any number of variables, with
any number of values in each (within reason: perhaps 20 variables
tops. The point is that the number is unknown and will change). Given
the data above, the correct answer would be:
1 45 100
2 45 100
3 45 100
1 60 100
2 60 100
3 60 100
1 45 200
2 45 200
3 45 200
1 60 200
2 60 200
3 60 200
Thanks!
| |
| John D'Errico 2005-04-26, 4:06 am |
| In article <ef03ff0.-1@webx.raydaftYaTP>, Chris <c-b@asu.edu> wrote:
> I have a problem that seems to me like it should be easy, but I can't
> get it to go. Suppose I have the following data:
>
> a = [1 2 3]
> b = [45 60]
> c = [100 200]
>
> What is the most efficient way to build a matrix such that each row
> is a unique combination of values from each variable? An important
> feature must be that it should handle any number of variables, with
> any number of values in each (within reason: perhaps 20 variables
> tops. The point is that the number is unknown and will change). Given
> the data above, the correct answer would be:
>
> 1 45 100
> 2 45 100
> 3 45 100
> 1 60 100
> 2 60 100
> 3 60 100
> 1 45 200
> 2 45 200
> 3 45 200
> 1 60 200
> 2 60 200
> 3 60 200
>
> Thanks!
Gosh, this should be easy. The poster wants to generate
all combinations of 20 variables or so, each taking on
3 levels. What will be the size of the resulting array?
8*20*3^20
ans =
5.5789e+11
That is bytes of memory required. I don't think you have
the address space for that many variables.
If you choose to limit the number a ways down, then ndgrid
will do most of the work for you. Then just unroll each
output variable into a single column of the result.
HTH,
John D'Errico
--
The best material model of a cat is another, or
preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
| |
|
| John D'Errico wrote:
> Gosh, this should be easy. The poster wants to generate
> all combinations of 20 variables or so, each taking on
> 3 levels. What will be the size of the resulting array?
>
> 8*20*3^20
> ans =
> 5.5789e+11
>
> That is bytes of memory required. I don't think you have
> the address space for that many variables.
Thanks for your reply. I typed 20 as a theoretical upper bound, not
as a working example. ndgrid was what I was looking for, thanks.
|
|
|
|
|