Re: Summary:Ways to get Odd Columns&Rows of Matrix
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg715] Re: Summary:Ways to get Odd Columns&Rows of Matrix
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Tue, 11 Apr 1995 13:25:08 GMT
- Organization: University of Virginia
In Message-ID: <3m6mj3$qnu at news0.cybernetics.net> Robby Villegas wrote:
>I think this general coding will work for higher tensors. The levels of
>the tensor get rotated around a full cycle, and end up where they were
>originally:
> everyother[tensor_List] :=
> With[{n = TensorRank[tensor]},
> Nest[Transpose[Part[#, Range[1, Length[#], 2]], RotateRight @ Range[n]]&,
> tensor, n]
> ]
This somewhat baroque function is equivalent to the simpler:
odd[tensor_List] := Part[ tensor, Sequence @@ (Range[1, #, 2] &) /@ Dimensions[tensor] ]
The simpler function also works for any tensor and it is faster. The
built-in optimized functionality of Part used with several indexing
lists is used in odd, but not in everyother. As Robby Villegas points
out, everyother is not meant as a practical solution.
In[3]:= u = Array[a, {5, 11, 20, 27}];
In[4]:= Timing[odd[u]][[1]]
Out[4]= 0.06 Second
In[5]:= Timing[everyother[u]][[1]]
Out[5]= 1.67 Second
In[6]:= everyother[u]==odd[u]
Out[6]= True
--
___________________________________________________________________________________
Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu
University of Virginia Department of Mechanical, Aerospace and Nuclear Engineering
___________________________________________________________________________________