Services & Resources / Wolfram Forums / MathGroup Archive
-----

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86393] Re: How to use Thread when second argument to function is a list
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 10 Mar 2008 02:04:12 -0500 (EST)
  • References: <fr0cqr$eh8$1@smc.vnet.net>

Nasser Abbasi schrieb:
> 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   f[ list , matrix]:=Module[{},.....]
> 
> I want to use Thread to call this function 'f' over all the rows of the 
> matrix. Something like
> 
> Thread[  f[{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[  f  {{1,2,3}, {matrix,matrix,matrix}} ]
> 
> and the above works but I have to duplicate 'matrix' to make it work, which 
> 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},
>    {nRow, nCol} = Dimensions[p];
>    lhs = Range[nRow];
>    Thread[findNonZeroElements[lhs, p]]      (* NOT WORKING *)
> ]
> 
> p = {{1, 0, 0, 0, 0}, {0, 0.2, 0.8, 0, 0}, {0, 0.7, 0.3, 0, 0},
>     {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,All]], 
> #1 != 0 & ]]
> 
> mchain[p_] := Module[{lhs, rhs},
>    {nRow, nCol} = Dimensions[p];
>     lhs = Range[nRow];
>     MapThread[findNonZeroElements, {lhs, Table[p, {nRow}]}]    (*OK*)
> ]
> 
> p = {{1, 0, 0, 0, 0}, {0, 0.2, 0.8, 0, 0}, {0, 0.7, 0.3, 0, 0},
>     {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
> 
> 

Hi Nasser,

this is a task for Map:

In[1]:=
findNonZeroElements[row_, p_] :=
   Select[p[[row]], #1 != 0 & ]

mchain[p_] :=
    (findNonZeroElements[#1, p] & ) /@ Length[p]]
p = {{1, 0, 0, 0, 0},
    {0, 0.2, 0.8, 0, 0},
    {0, 0.7, 0.3, 0, 0},
    {0.1, 0, 0.1, 0.4, 0.4},
    {0, 0.1, 0.3, 0.2, 0.4}};
mchain[p]
Out[4]=
{{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}}

or simply:

Clear[mchain]
mchain[m_]:=MSelect[#, # != 0 &] & /@ m

Peter


  • Prev by Date: Re: How to use Thread when second argument to function is
  • Next by Date: RE: Re: Re: Version 6.0.2
  • Previous by thread: Re: How to use Thread when second argument to function is a list
  • Next by thread: Re: How to use Thread when second argument to function is a list