[Date Index]
[Thread Index]
[Author Index]
Summary:Ways to get Odd Columns&Rows of Matrix
*To*: mathgroup at christensen.cybernetics.net
*Subject*: [mg643] Summary:Ways to get Odd Columns&Rows of Matrix
*From*: Xah Y Lee <xyl10060 at fhda.edu>
*Date*: Wed, 5 Apr 1995 00:32:13 -0700 (PDT)
This is a summary of Ways to get only odd rows and columns of a matrix.
Problem: Suppose we have a mxn matrix. We want to cross out the even rows
and columns, and get the remaining matrix. How to do that?
This is built-in in mma. The simplest way to do it is
Part[myMatrix,{1,3,5...},{1,3,5...}]. See section 3.7.2, p.651 of the mma
book.
Below is a collection of various ways to write this program. Some of them
are general that can work on matrices with more than 2 dimensions, and
user can choose odd only, even only, or generalized such as every third
member in the tensor. Most others are included not as a practical solution but
as programming examples.
These pieces of codes are contributed by various members of mathgroup.
I've taken the liberty to modify some of them to make this a coherent
summary.
------------
Clear[myList]
myList = Array[a,{10,18}];
------------
(*Using Part*)
(*From: Robert Villegas <villegas at wri.com>*)
(*From: Count Dracula <lk3a at kelvin.seas.virginia.edu>*)
(*
In my opinion this is the best solution.
Use Map[Select[Range[#], OddQ] &, Dimensions[myMatrix]] to generate a
list {{1,3,5,..}, {1,3,5,7...}}, then feed it to Part to extract, like
this
Part[myMatrix, {1,3,5}, {1,3,5,7}].*)
*)
MatrixSieve1a[myMatrix_] :=
Part[myMatrix,
Sequence@@ Map[ Range[1, #, 2]&, Dimensions[myMatrix]]
]
MatrixSieve1b[myMatrix_, sel_:OddQ] :=
Part[myMatrix,
Sequence@@ Map[(Select[Range[#], sel] &) , Dimensions[myMatrix]]
]
------------
(*Using Table.*)
(*From: Lou Talman <me at talmanl.mscd.edu>*)
(*easiest to understand.*)
MatrixSieve2[myMatrix_] :=
Module[{i,j},
Table[ myMatrix[[i, j]],
{i, 1, Length[myMatrix], 2},
{j, 1, Length[First@myMatrix], 2}
]
]
------------
(*using Partition. *)
(*From: Xah Lee <74631.731 at compuserve.com>*)
(*
This one is hard to understand. It's not easy to generalize as well. It
will not work if the matrix contain odd number of row or column, but it's
got the potential to become the most elegant solution. It's the fastest
among all.
*)
MatrixSieve3[myMatrix_] := Map[ First, Partition[myMatrix,{2,2}],{2,3}]
------------
(*Recursively elimate the second term.*)
(*From: Tyler Perkins <perkins at colorado.edu>*)
(*
Define OddOnly[], that will get rid of the second element in a list.
This is repeated recursively in a nested way so that at the end only the
odd terms of the list is left.
Map OddOnly to every level of myList.
*)
OddOnly[{first_, second_, rest___}] := Join[{first}, OddOnly[{rest}]];
OddOnly[singleExpr_] := singleExpr;
MatrixSieve4[myMatrix_] := MapAll[ OddOnly, myMatrix]
------------
(*using Array and Part*)
(*From: "L. Hannibal" <hannibal at caesar.physik.uni-oldenburg.de>*)
MatrixSieve5[myMatrix_]:=
Array[
myMatrix[[2 #1 -1,2 #2-1]]&,
{Ceiling[Length[myMatrix]/2], Ceiling[Length@First@myMatrix/2]}
]
------------
(*using Pattern and ReplaceRepeated*)
(*From: Robert Zimmerman <bob at zim.uoregon.edu>*)
(*This is not a general method, but included here as a extra example of
programming techniques. It only works on matrices whos elements have the
form head[x,y]. It works by first flatten myList, then replace elements
with even index with 0, then eliminate the zeros in the list. Lastly,
Partition them into the correct form.
*)
Partition[
(Flatten@myList)//.{a[x_ ,y_?EvenQ ] ->0, a[x_?EvenQ ,y_ ] ->0}//.
{ head___ ,0, tail___ } :> {head, tail}
,3
]
------------
(* using Fold and Drop *)
(*From: bob Hanlon <hanlon at pafosu2.hq.af.mil>*)
(*works on matrices with even number of terms.
Not a good example of coding.
The use of Fold, Transpose and Drop are too complex.
Slow as well.
*)
Transpose[
Fold[ Drop,
Transpose[
Fold[ Drop, myList,
-Array[List,Length@myList/2]
]
],
-Array[List,Length@First@myList/2]
]
]
------------
Xah Lee
Come! Have compassion toward your fellow human beings! Please send $1 to
650 Alamo CT APT#1, MTN View, CA 94043, US of A to support a future
mathematician to pay off his dentist. You surely agree that teeth are
more important then Mathematica?
Prev by Date:
**.math22prefs.mb file getting corrupted**
Next by Date:
**WWW sites**
Previous by thread:
**.math22prefs.mb file getting corrupted**
Next by thread:
**Re: Summary:Ways to get Odd Columns&Rows of Matrix**
| |