Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Matrix Element Extraction

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46650] RE: [mg46622] Matrix Element Extraction
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Thu, 26 Feb 2004 17:54:01 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Curt Fischer [mailto:crf3 at po.cwru.edu]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, February 25, 2004 7:07 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg46650] [mg46622] Matrix Element Extraction
>
>
>In a matrix, I want to find the positions of all the integer 
>elements in the
>rightmost column or the bottom row.
>
>In[1]:=
>mat={{1,2,1},{4,5,6},{7,8,-Infinity}}
>
>Out[1]=
>{{1,2,1},{4,5,6},{7,8,-Infinity}}
>
>In[2]:=
>Position[Map[((MemberQ[Union[Flatten[{mat[[Length[mat]]],
>                mat[[All,Dimensions[mat][[
>                      2]] ]]}]],#])&&(#!=-Infinity))&,mat,{2}],True]
>
>Out[2]=
>{{1,1},{1,3},{2,3},{3,1},{3,2}}
>
>This _mostly_ does what I want, but took me forever to think of.  I'm
>convinced there must be an easier way...
>
>Especially, is there an easy way to keep the point {1,1} from 
>appearing on
>this
>list, other than using a replacement rule and an If[] command? 
> ({1,1} is
>not a position in the bottom row or right most column, so I 
>don't want this
>point returned.)
>
>Thanks for any advice.
>
>-- 
>Curt Fischer
>
>

Curt,

perhaps the most simple way to do it is to find out all positions of integer
elements and then filter out those of the last row or line:

{d1, d2} = Dimensions[mat]; 

Cases[Position[mat, _Integer, {2}], {d1, _} | {_, d2}]

==> {{1, 3}, {2, 3}, {3, 1}, {3, 2}}

(the level specification for Position is essential)



If you want to find the positions of the non-integer elements there, use

Cases[Position[mat, _?(! IntegerQ[#] &), {2}, 
    Heads -> False], {d1, _} | {_, d2}]

==> {{3, 3}}



Alternatively you might consider, the last row or line separately:

Append[#, d2] & /@ Position[mat[[All, -1]], _Integer, {1}]

==> {{1, 3}, {2, 3}}


Prepend[#, d1] & /@ Position[mat[[-1]], _Integer, {1}]

==> {{3, 1}, {3, 2}}


--
Hartmut Wolf


  • Prev by Date: Re: a question about [[ ]]
  • Next by Date: Re: More Pattern Match Understanding Problems
  • Previous by thread: Re: Matrix Element Extraction
  • Next by thread: More Pattern Match Understanding Problems