简体   繁体   中英

Arrayfun problems in matlab

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?

Use this :

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

To access use :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM