Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Transpose[X, (3,1,2}]' topicStudent Support Forum > General > Archives > "Transpose[X, (3,1,2}]"

Next Comment >Help | Reply To Topic
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: ,

Subject (listing for 'Transpose[X, (3,1,2}]')
Author Date Posted
Transpose[X, (3,1,2}] Brian McFann 10/20/01 5:53pm
Re: Transpose[X, (3,1,2}] Forum Modera... 10/28/01 9:19pm
Next Comment >Help | Reply To Topic