[Date Index]
[Thread Index]
[Author Index]
How to use Thread when second argument to function is a list of lists?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg86372] How to use Thread when second argument to function is a list of lists?
*From*: "Nasser Abbasi" <nma at 12000.org>
*Date*: Sun, 9 Mar 2008 05:06:48 -0500 (EST)
*Reply-to*: "Nasser Abbasi" <nma at 12000.org>
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: changing style of vertices for ShowGraph with Combinatorica**
Next by Date:
**Re: Assignment problem**
Previous by thread:
**Re: How to use Thread when second argument to function is a list of lists?**
Next by thread:
**Create pdf document from graphics**
| |