Re: Matrix Element Extraction
- To: mathgroup at smc.vnet.net
- Subject: [mg46654] Re: [mg46622] Matrix Element Extraction
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 26 Feb 2004 17:54:17 -0500 (EST)
- References: <200402251807.NAA05947@smc.vnet.net> <D3415A22-6825-11D8-B618-00039311C1CC@mimuw.edu.pl> <06A659F9-6828-11D8-B618-00039311C1CC@mimuw.edu.pl>
- Sender: owner-wri-mathgroup at wolfram.com
On 26 Feb 2004, at 07:50, Andrzej Kozlowski wrote: > > On 26 Feb 2004, at 07:34, Andrzej Kozlowski wrote: > >> On 25 Feb 2004, at 19:07, Curt Fischer wrote: >> >>> 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 >>> >>> >>> >> How about this: >> >> f[mat_?MatrixQ] := Block[{d = Dimensions[mat], dog, cat}, >> Join @@ Last[Reap[MapIndexed[If[IntegerQ[#1] && (First[#2] == >> First[ >> d] || Last[#2] == Last[d]), Sow[#2, dog], Sow[#2, cat]] &, mat, >> {2}], >> dog]]] >> >> >> Now with: >> >> mat = {{1, 2, 1}, {4, 5, 6}, {7, 8, -Infinity}}; >> >> >> f[mat] >> >> >> {{1,3},{2,3},{3,1},{3,2}} >> >> > > Of course if you are not interested in collecting both kinds of > elements then all you need is > > f[mat_?MatrixQ] := Block[{d = Dimensions[mat]}, Join @@ > Last[Reap[MapIndexed[If[IntegerQ[#1] && (First[#2] == > First[d] || Last[#2] == Last[d]), Sow[#2]] &, mat, {2}]]]] > > Andrzej On second thoughts I realized that the above code, while in my opinion quite elegant, is hardly efficient for this sort of problem since the MapIndexed "looks" at every position in the matrix while we know that the elements we are interested in are all in the last column and the last raw. So, for large matrices, the following ought to do better: f[mat_?MatrixQ] := With[{f = First[Dimensions[mat]], g = Last[Dimensions[mat]]}, Cases[Join[Transpose[{ mat[[All, g]], Table[{i, g}, {i, g}]}], Transpose[{mat[[f, All]], Table[{f, j}, {j, f}]}]], {_Integer, p_List} :> p, Infinity]] f[mat] {{1,3},{2,3},{3,1},{3,2}} Andrzej Kozlowski Chiba, Japan http://www.mimuw.edu.pl/~akoz/
- References:
- Matrix Element Extraction
- From: "Curt Fischer" <crf3@po.cwru.edu>
- Matrix Element Extraction