I have a 2D array of complex values and I want to apply a function to every entry of the array, for every timepoint t in a list. I know how to apply my function to one entry for every time t in a list, and I know how to apply my function for one t to every entry in an array. (I keep finding answers to those problems when searching with google and on SE) Somehow I cant get both to work at the same tim, and I cant find an explanation of how to do it either. Maybe someone has experience in doing this?


function[source_, t_] := source(1+Sin[2Pi*t]);
lena = Import["ExampleData/lena.tif"];
size = ImageDimensions[lena];
data = ImageData[lena];
times = Table[i, {i, 600}]/10;
complexred = Fourier[data[[1;;size[[2]],1;;size[[1]],1]]];
newdata = function[complexred, times];

Thread::tdlen : Objects of unequal length in 
{{...some complex values here...}{<<1>>},<<48>>,<<430>>} cannot be combined.


  • $\begingroup$ f(x) means f*x. Compare ImageDimensions[lena] and Dimensions[data] $\endgroup$
    – ssch
    Sep 21, 2013 at 23:50
  • $\begingroup$ Ah those brackets were a typo, sorry. Matlab automatisms :). Thank you for pointing out the difference in coordinates for images and matrices! Thats useful to me. $\endgroup$
    – Leo
    Sep 22, 2013 at 8:40

2 Answers 2


Consider this:

fn[img_Image][t_] := ImageApply[Evaluate[# (1 + Sin[2 Pi*t])] &, img]

times = Table[i, {i, 0, 1, 0.1}];

fn[lena] /@ times

enter image description here

I used SubValues notation for flexibility (direct mapping onto times) but it is not required. You can also use the function like this:

Table[fn[lena][i], {i, 0, 1, 0.1}]

I pre-evaluated the body of the Function using Evaluate for best speed, but this is also not required. (Note that Evaluate must be applied to the entire body of a Function for it to have the desired effect; to evaluate a piece of the body use With.)

  • $\begingroup$ Thank you for answering my question and explaining how an why you perform each step. Nice and illustrative. I take it the /@ operator can be applied to arrays of complex numbers as well, as thats what bill s does. I dont quite understand the #*body &, img part yet. What do the # and & do there? $\endgroup$
    – Leo
    Sep 22, 2013 at 8:58
  • $\begingroup$ @Leo You're welcome. Let me know if you have any other questions about what I wrote. Yes, /@ which is short for Map can be applied to any expression, with control over level using the full notation Map[func, expr, levelspec]. Bookmark this post for reference. You can also select an operator such as /@, # or & in the Front End and press F1 to bring up the help page for it. (continued) $\endgroup$
    – Mr.Wizard
    Sep 22, 2013 at 9:04
  • $\begingroup$ # is short for Slot, and & is short for Function; they are the basic building blocks of anonymous functions in Mathematica. See this and linked resources for an explanation of levelspec. $\endgroup$
    – Mr.Wizard
    Sep 22, 2013 at 9:05
  • $\begingroup$ Both links are very useful, thanks. $\endgroup$
    – Leo
    Sep 22, 2013 at 9:13
  • $\begingroup$ @Leo I changed my With construct to Evaluate, which in this particular case is less baroque. Be careful to understand how Evaluate works before attempting to apply it elsewhere. $\endgroup$
    – Mr.Wizard
    Sep 22, 2013 at 10:41

You say that your data has the form of complex values, so I will make up some complex-valued data and a range of time:

data = RandomReal[{-1, 1}, {10, 10}] + I RandomReal[{-1, 1}, {10, 10}];
time = Range[0, 1, 0.05];

Now consider a function

f[x_, t_] := Abs[x]^t

The goal is to "apply the function to every element of the data, for every timepoint." One way to do this is:

g = f[data, #] & /@ time

which gives an array of size

{21, 10, 10}

This makes sense since there are 21 points in the time list and the data is a 10x10 array.

  • $\begingroup$ Thanks you for answering my question! Although strictly your answer more directly answers my question than mr.wizards (by using complex numbers and explaining the resulting array), I did accept his answer because both your answers are actually the same method and his answer also showed me some extra tricks for using /@ effectively. But I like your explanation about the dimensions of the resulting array, since that was in my question as well. So thanks. $\endgroup$
    – Leo
    Sep 22, 2013 at 8:47

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.