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?

1 Answer 1

1

Use this :

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

To access use :

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Will it calulate the factors all at once, or not until I write l{2}?
@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
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.