1 view (last 30 days)

Hi everyone

How can I evalute a randomly generated equation as String,without using eval, to speed up my code

like:

'0.81472/mask(4)+mask(16)*mask(7)/mask(24)/mask(4)-mask(24)+mask(21)/mask(11)/mask(20)+mask(17)/mask(22)/mask(17)-mask(19)+mask(17)+mask(18)+mask(7)/mask(3)-mask(18)+mask(24)-mask(11)/mask(20)-mask(5)*mask(12)/mask(18)*mask(7)+mask(17)-mask(3)'

ps mask is an image mask 5*5 with changable values.

rubindan
on 27 May 2020

You can use str2func as follows:

rnstr = ['0.81472/mask(4)+mask(16)*mask(7)/mask(24)/mask(4)',...

'-mask(24)+mask(21)/mask(11)/mask(20)+mask(17)/mask(22)/mask(17)',...

'-mask(19)+mask(17)+mask(18)+mask(7)/mask(3)-mask(18)+mask(24)',...

'-mask(11)/mask(20)-mask(5)*mask(12)/mask(18)*mask(7)+mask(17)-mask(3)'];

str = ['@(mask) ',rnstr]; % add "mask" as the input

fh = str2func(str) % a function handle

val = fh(rand(1,50)); % value for some rnadom input

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 18 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864023

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864023

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864058

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864058

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864078

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864078

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864193

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864193

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864323

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_864323

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865343

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865343

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865708

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865708

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865718

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_865718

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866088

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866088

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866123

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866123

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866223

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866223

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866353

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866353

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866368

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866368

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866393

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866393

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866443

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866443

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866488

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866488

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866968

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_866968

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_867023

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/533528-functions-instead-eval#comment_867023

Sign in to comment.