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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86405] Re: [mg86372] How to use Thread when second argument to function is
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 10 Mar 2008 02:06:27 -0500 (EST)
  • Reply-to: hanlonr at cox.net

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}};

Select[#, # != 0 &] & /@ p

{{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}}

Cases[#, _?(# != 0 &)] & /@ p

{{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}}

p /. 0 -> Sequence[] (* space required after /. *)

{{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}}

% == %% == %%%

True


Bob Hanlon

---- Nasser Abbasi <nma 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   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
> 
> 



  • Prev by Date: Re: Re: changing style of vertices for ShowGraph with Combinatorica
  • Next by Date: Re: How to use Thread when second argument to function is a list
  • Previous by thread: Re: FindFit issue
  • Next by thread: Auto Export to XHTML+MathML on Save