Say I have matrix tmp1 of size 1000 x 5 (shown is just a small section for illustration purpose), consisting of real numbersenter image description here and 0:

How can I iteratively take every non-zero real number elements and use it as input for another 'For' loop argument limit to calculate another set of matrix values? For example, taking second row, first column value of 4.956 I would like to use it to calculate a 'For' loop like


For[i = 0, i < 4.956, i = i + 0.2,

calculate P = Exp[-r i]*i

P += P

And place this cumulated P result into tmp2 output matrix at same corresponding location as where 4.956 was taken from. Then repeat for the next non-zero element of tmp1, so for row 2, column 2 value of 1.234:

For[i = 0, i < 1.234, i = i + 0.2,

calculate P = Exp[-r i]*i

P += P

and result into tmp2 location row 2, column 2. Any zero elements found in tmp1 will simply be mapped to tmp2 of same location. Thanks.

  • $\begingroup$ Did you consider the answers from this question mathematica.stackexchange.com/questions/14262/…? $\endgroup$ Commented Nov 14, 2012 at 8:00
  • $\begingroup$ For any question you ask, below the voting arrows is a checkmark. If you find an answer the most useful of the one's you received, click on the check mark. It then turns green, like on this answer to one of your questions. Note, your selection is not set in stone, if another answer comes along that is better, you can change it. $\endgroup$
    – rcollyer
    Commented Nov 15, 2012 at 3:37

2 Answers 2


Here are 3 approaches to appling a function to a 2D list ( matrix ) of scalar values.

myFunc[x_] := x^2

matrix = RandomInteger[Range@2, {4, 5}];

Mathematica graphics

Listable Function Attribute

SetAttributes[myFunc, Listable]


Mathematica graphics


As Andy Ross has illustrated you can use Map.

Map[myFunc, matrix, {2}]

Mathematica graphics

Replacement Rule(s)

matrix /. m_ -> myFunc[m]

Mathematica graphics


Define a larger matrix of 10^7 elements, apply the function and measure the AbsoluteTiming.

matrix = RandomInteger[Range@2, {1000, 10000}];
Grid[{{"Map", "Listable", "Replacement"}, 
{ Map[myFunc, matrix, {2}]; // AbsoluteTiming,
  Clear@m; myFunc@matrix; // AbsoluteTiming,
  matrix /. m_ -> myFunc[m]; // AbsoluteTiming}
 [[All, 1]]
}, Dividers -> All]

Mathematica graphics

  • $\begingroup$ Hi @image_doctor could you share the code for coding the last part of AbsoluteTiming? Thanks $\endgroup$ Commented Nov 14, 2012 at 13:40
  • $\begingroup$ @sebastiancheung Yes of course, on re-running it I noticed a small error which changes the results so that Listable and Replacement are now much closer in performance. $\endgroup$ Commented Nov 14, 2012 at 16:03
  • $\begingroup$ Your choice of x^2 for myFunc makes these timings a little misleading. You should try a function that isn't already listable like, for example, myFunc[x_] := Total[Range[x]]. It is worth looking at TracePrint of your Replacement example to see why this matters. $\endgroup$
    – Andy Ross
    Commented Nov 14, 2012 at 18:01
  • $\begingroup$ @AndyRoss Perhaps it makes more sense to call the 3rd option Replacement + Listable. $\endgroup$ Commented Nov 14, 2012 at 18:11

You could use a bit of functional programming. Here I'm fixing r to be 3 for readability.

With[{r = 3}, 
   Map[(#.Exp[-r*#]) &[Range[0, #, .2]] &, tmp1, {2}]

(*{{0., 0., 0.}, {0.539181, 0.484569, 0.534811}, {0.484569, 0., 
  0.539174}, {0., 0., 0.484569}}*)

This is likely quite a bit faster than anything you are going to get with For loops unless you use Compile. Here's a benchmark.

tmp = RandomChoice[{0, 1}, {10^5, 10}]*RandomReal[{0, 5}, {10^5, 10}];

With[{r = 3}, 
   Map[(#.Exp[-r*#]) &[Range[0, #, .2]] &, 
    tmp, {2}]]; // AbsoluteTiming

(*{1.160002, Null}*)


In response to the comments. Here is one way to do this using ReplaceAll. Note that it is about 10 times slower than using Map.

With[{r = 3},
   SetAttributes[fn, Listable];
   fn[m_] := (#.Exp[-r*#]) &[Range[0, m, .2]];
   tmp /. m_ :> fn[m]
   ]; // AbsoluteTiming

(*{10.6900150, Null}*)

Finally, if you really want a more procedural approach you can use Compile with loops. I've pulled out all the stops here to make it fast. If you have a C compiler installed you could additionally try setting the CompilationTarget to "C".

f = Compile[{{r, _Real}, {tmp, _Real, 2}},
   Block[{g, rng, u, v, out = Internal`Bag[], res},
    {v, u} = Dimensions[tmp];
     rng = Range[0., Compile`GetElement[tmp, i, j], .2];
     res = rng.Exp[-r*rng];
     Internal`StuffBag[out, res]
     , {i, v}, {j, u}
    Partition[Internal`BagPart[out, All], u]
    ], RuntimeOptions -> "Speed"];

Note that this didn't make things much faster and the functional approach is in my opinion much cleaner.

f[3., tmp]; // AbsoluteTiming

(*{0.9984018, Null}*)
  • $\begingroup$ Hi @Andy Ross, the last {2} is the levelspec? Not sure how that affects the results? $\endgroup$ Commented Nov 14, 2012 at 15:23
  • $\begingroup$ Thank @Andy Ross works well, not sure if I want to use Replacement rule instead of Map when working with large data set later. $\endgroup$ Commented Nov 14, 2012 at 16:07
  • $\begingroup$ Yes, the {2} is the levelspec. I recommend using Map[f, Array[Subscript[x, ##] &, {5, 5}], lspec] with different lspecs to see what this does. $\endgroup$
    – Andy Ross
    Commented Nov 14, 2012 at 17:42
  • $\begingroup$ Hi @Andy Ross, so how to modify your solution to use Replacement instead of Map? Performance seems faster using it as image_doctor pointed out. :) $\endgroup$ Commented Nov 15, 2012 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.