MathGroup Archive 2008

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

Search the Archive

Re: How to use Thread when second argument to function is a list of

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86424] Re: How to use Thread when second argument to function is a list of
  • From: Januk <ggroup at sarj.ca>
  • Date: Tue, 11 Mar 2008 02:57:19 -0500 (EST)
  • References: <fr0cqr$eh8$1@smc.vnet.net>

Hi Nasser,

You could use a single ReplaceAll command to get the same result:
p /. { _?( # == 0.& ) :> Sequence[] }

Good luck!

On Mar 9, 6:06=A0am, "Nasser Abbasi" <n... at 12000.org> wrote:
> Hello;
>
> I have a function which I want to call repeatedly for all rows of a matrix=

> to find non-zero elements in each row.
> This function takes as input the row number and the matrix itself and
> returns a list that contain the non-zero elements in the that row.
>
> i.e. function is =A0 f[ list , matrix]:=Module[{},.....]
>
> I want to use Thread to call this function 'f' over all the rows of the
> matrix. Something like
>
> Thread[ =A0f[{1,2,3}, matrix] ]
>
> But it is not working. I guess becuase matrix is a list of lists and it is=

> of different dimension than the list of the row numbers?
> I can use MapThread, but then I have to write
>
> MapThread[ =A0f =A0{{1,2,3}, {matrix,matrix,matrix}} ]
>
> and the above works but I have to duplicate 'matrix' to make it work, whic=
h
> I do not like.
>
> Here is the code that does not work
>
> ------------------- Thread not working, why? -----------------
>
> findNonZeroElements[row_, p_] := Module[{}, Select[p[[row,All]], #1 !==
 0
> & ]]
>
> mchain[p_] := Module[{lhs, rhs},
> =A0 =A0{nRow, nCol} = Dimensions[p];
> =A0 =A0lhs = Range[nRow];
> =A0 =A0Thread[findNonZeroElements[lhs, p]] =A0 =A0 =A0(* NOT WORKING *)
> ]
>
> p = {{1, 0, 0, 0, 0}, {0, 0.2, 0.8, 0, 0}, {0, 0.7, 0.3, 0, 0},
> =A0 =A0 {0.1, 0, 0.1, 0.4, 0.4}, {0, 0.1, 0.3, 0.2, 0.4}};
>
> mchain[p]
> ---------------------------------------------------------
>
> Here is the same as above but using MapThread and this works
>
> In[172]:= findNonZeroElements[row_, p_] := Module[{}, Select[p[[row,Al=
l]],
> #1 != 0 & ]]
>
> mchain[p_] := Module[{lhs, rhs},
> =A0 =A0{nRow, nCol} = Dimensions[p];
> =A0 =A0 lhs = Range[nRow];
> =A0 =A0 MapThread[findNonZeroElements, {lhs, Table[p, {nRow}]}] =A0 =A0(*O=
K*)
> ]
>
> p = {{1, 0, 0, 0, 0}, {0, 0.2, 0.8, 0, 0}, {0, 0.7, 0.3, 0, 0},
> =A0 =A0 {0.1, 0, 0.1, 0.4, 0.4}, {0, 0.1, 0.3, 0.2, 0.4}};
> mchain[p]
>
> Out[175]= {{1}, {0.2, 0.8}, {0.7, 0.3}, {0.1, 0.1, 0.4, 0.4}, {0.1, 0.3,=

> 0.2, 0.4}}
>
> thanks,
> Nasser



  • Prev by Date: Re: A question regarding a hyperbolic geometric function
  • Next by Date: Re: Determinant and Characteristic Polynomial not working properly
  • Previous by thread: Paclets
  • Next by thread: change the position of documentaiotnal center in v6