| Author |
Comment/Response |
Brian McFann
|
10/20/01 5:53pm
I want to know why Transpose doesn't give me the results I expect. Consider the following matrix:
In:
X = Array[x, {2, 3, 4}]
Out:
{{{x[1, 1, 1], x[1, 1, 2], x[1, 1, 3], x[1, 1, 4]}, {x[1, 2, 1], x[1, 2, 2], x[1, 2, 3], x[1, 2, 4]}, {x[1, 3, 1], x[1, 3, 2], x[1, 3, 3], x[1, 3, 4]}},
{{x[2, 1, 1], x[2, 1, 2], x[2, 1, 3], x[2, 1, 4]}, {x[2, 2, 1], x[2, 2, 2], x[2, 2, 3], x[2, 2, 4]}, {x[2, 3, 1], x[2, 3, 2], x[2, 3, 3], x[2, 3, 4]}}}
Suppose it represents data on 2 variates, for 3 countries, over 4 years. I want to regroup so that the data is stacked by year, then variate, and then country. So I try Transpose:
In:
Transpose[X, {3, 1, 2}]
Out:
{{{x[1, 1, 1], x[2, 1, 1]}, {x[1, 1, 2], x[2, 1, 2]}, {x[1, 1, 3], x[2, 1, 3]}, {x[1, 1, 4], x[2, 1, 4]}},
{{x[1, 2, 1], x[2, 2, 1]}, {x[1, 2, 2], x[2, 2, 2]}, {x[1, 2, 3], x[2, 2, 3]}, {x[1, 2, 4], x[2, 2, 4]}},
{{x[1, 3, 1], x[2, 3, 1]}, {x[1, 3, 2], x[2, 3, 2]}, {x[1, 3, 3], x[2, 3, 3]}, {x[1, 3, 4], x[2, 3, 4]}}}
From the definition of Transpose, I expected that it would group all those elements with the same third index number in the same first-level sublist of the results. But it didn't
I wanted this:
{{{x[1, 1, 1], x[1, 2, 1], x[1, 3, 1]}, {x[2, 1, 1], x[2, 2, 1], x[2, 3, 1]}},
{{x[1, 1, 2], x[1, 2, 2], x[1, 3, 2]}, {x[2, 1, 2], x[2, 2, 2], x[2, 3, 2]}},
{{x[1, 1, 3], x[1, 2, 3], x[1, 3, 3]}, {x[2, 1, 3], x[2, 2, 3], x[2, 3, 3]}},
{{x[1, 1, 4], x[1, 2, 4], x[1, 3, 4]}, {x[2, 1, 4], x[2, 2, 4], x[2, 3, 4]}}}
Which I can get this way:
MapThread[List, Map[MapThread[List, #] &, X]]
But why doens't Transpose[X, {3, 1, 2}] give me what I expect?
Thanks
URL: , |
|