1

I'm trying to do a simple speed test in Matlab. I want it to factor 3^a-2 for each a from 1 to 20. Might be I'm choosing too small numbers here to see any significant difference, but I'm stuck anyway.

I tried to write

n = [1:20]
m = 3.^n-1
arrayfun(factor,m)

this gives a "not enough input parameters" error. I though it made sense but apparently not. After looking at some examples of arrayfun and the manual, i also tried

arrayfun(@(m)factor(m), m)
arrayfun(@(m), factor(m), m)
arrayfun(@factor, m)

but none worked. What's the correct way to do it? And also if I do speed tests of this sort, will the results be cached so I will have to use different numbers if I do the test again?

user1661303
  • 539
  • 1
  • 7
  • 20

1 Answers1

1

Use this :

l=arrayfun(@factor,m, 'UniformOutput', false);

To access use :

l{1}, l{2}...etc

P0W
  • 46,614
  • 9
  • 72
  • 119
  • Thanks. Will it calulate the factors all at once, or not until I write l{2}? – user1661303 Sep 28 '13 at 12:10
  • @user1661303 yes it will calculate all factors at once, you can check that by omitting the semi-colon at last. This will display cells of each elements – P0W Sep 28 '13 at 12:13
  • OK, thanks. Just one more question. What's the difference in this context of writing l(2) and l{2}? I notice the output is different. – user1661303 Sep 28 '13 at 12:35