Re: Rearranging a tensor
- To: mathgroup at smc.vnet.net
- Subject: [mg22448] Re: [mg22433] Rearranging a tensor
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sun, 5 Mar 2000 00:23:44 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I can't think of a *really simple* way. But if yoou load in the package
<< LinearAlgebra`MatrixManipulation`
you can define something like:
In[3]:=
transform1[l_] :=
Module[{s = Dimensions[l]},
Transpose[
Apply[AppendColumns,
Table[Apply[AppendRows, Table[l[[i, j]], {i, 1, s[[1]]}]], {j, 1,
s[[2]]}]]]]
then taking your definition of c we get:
In[10]:=
transform1[c] // MatrixForm
Out[10]//MatrixForm=
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
You can also do this without the package:
In[11]:=
transform2[l_] :=
Module[{s = Dimensions[l]},
Flatten[Map[Transpose,
Partition[Partition[Flatten[l], Last[s]], s[[2]]*s[[-2]]]], 1]]
In[12]:=
transform2[c] // MatrixForm
Out[12]//MatrixForm=
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
One can generalize this to higher tensors but it soon gets complicated.
on 3/1/00 6:40 AM, DIAMOND Mark at noname at noname.com wrote:
> I have
>
> In[1]:= a={{1,2},{3,4}};
>
> In[2]:= b=Table[1,{3},{3}];
>
> In[3]:= c=Outer[Times,a,b]
>
> Out[3]= {{{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}},
>
>> {{2, 2, 2}, {2, 2, 2}, {2, 2, 2}}},
>
>> {{{3, 3, 3}, {3, 3, 3}, {3, 3, 3}}, {{4, 4, 4}, {4, 4, 4}, {4, 4, 4}}}}
>
> In[4]:= c//MatrixForm
>
> Out[4]//MatrixForm=
> 1 1 1 2 2 2
> 1 1 1 2 2 2
> 1 1 1 2 2 2
>
> 3 3 3 4 4 4
> 3 3 3 4 4 4
> 3 3 3 4 4 4
>
> I would now like to rearrange c so that it is of the form
> {{1,1,1,2,2,2},{1,1,1,2,2,2},{1,1,1,2,2,2},{3,3,3,4,4,4},{ 3,3,3,4,4,4},{3,3
> ,3,4,4,4}}
>
> There must be a *simple* way to do this but I cannot see it. Help would be
> appreciated.
>
> Cheers,
>
> Mark R Diamond
> Vision Research Laboratory
> The University of Western Australia
> no spam email: markd at psy dot uwa dot edu dot au
>
>
>
>
--