functions instead eval?
Show older comments
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.
18 Comments
Image Analyst
on 26 May 2020
Edited: Image Analyst
on 26 May 2020
That should be lightning fast.
mask = randi(9, 5, 5)
tic
output = 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)'
toc
output =
14.1936882539683
Elapsed time is 0.000295 seconds.
Exactly just how slow is it on your system, and how fast to you need it? Doing it in 0.2 milliseconds seems pretty damn fast to me. 🚀
Edit: Oh, you're starting with a string rather than a line of code. I don't know of anyway to evaluate it without using the dreaded eval(). Still, just how long is it taking on your system? How did it end up as a string in the first place? That is what you should correct.
Steven Lord
on 26 May 2020
Is there a reason you are storing the equations as char vectors? Why not store them in MATLAB function files?
function out = myfun(mask)
out = 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);
end
The critical part of this question is that it is a "a randomly generated equation", which means that essentially you are asking MATLAB to be a MATLAB code parser... and eval can be the easiest way to do that in the general case. But if your equations have any kind of systematic features then you might be able to leverage those into a more efficient way of representing and evaluating those equations. For example, the allowed operators, any limits on the number of terms, the order of terms, etc.
Mohammad Sami
on 26 May 2020
Edited: Mohammad Sami
on 26 May 2020
Is the entire equation randomly generated, or only the data used in equation is randomly generated.
Mohammad Sami
on 26 May 2020
I had come across a previous question, where the user was using matlab to write code to a .m file, then simply execute that file at the end. Assuming that you can write all the equations first, then execute them at the end, this may work for you. Do note this method cannot be compiled.
Bjorn Gustavsson
on 26 May 2020
What type of rules do you have for the generation of the randomly generated equation? What is the objective of the calculation? If it is some truely random sum of products as a bove, why can't you simply replace them with an arbitrary random number and claim that's what you got?
eng had
on 26 May 2020
Rik
on 26 May 2020
This may sound stupid, but isn't that what machine learning is for? There is a toolbox full of relevant functions. Why try to re-invent the wheel?
eng had
on 26 May 2020
Rik
on 26 May 2020
There is no need for SHOUTING. Also, if you want to insult people, please go elsewhere.
You haven't given me any indication yet why your application in genetic programming can't use normal deep learning tools. This string of divisions and additions doesn't seem outside the realm of possibilities of what you can achive with a CNN.
eng had
on 26 May 2020
Bjorn Gustavsson
on 26 May 2020
I have to repeat the question: What rules do you use to generate your expressions? Just some random sums of products between elements in your mask? Your purpose is not enough to give us enough information to help you. You might get something like your initial expression from a modification of Steven Lord's function:
function out = myfun(mask,factors,Indices,Powers)
out = 0;
for i1 = 1:numel(factors)
C = factors(i1);
for i2 = 1:numel(Indices{i1})
C = C*mask(Indices{i1}(i1)).^Powers{i1}(i2);
end
out = out+C;
end
If that's enough I cannot tell, you have to give us more information...
Bjorn Gustavsson
on 26 May 2020
You should be able to convert that random generation to generate arrays with Indices and exponents to put into cell-arrays for use in the function above.
Good luck.
per isakson
on 27 May 2020
This might be a XY Problem. Instead of trying to evaluate an expression, which is representeted by a string, it might be better to search for a more useful way to represent the expression.
Image Analyst
on 27 May 2020
Edited: Image Analyst
on 27 May 2020
You assigned the tag "image processing". Why? I don't see any image processing in what you've presented so far. I don't see how assigning random operations will help you "detect the relation between the pixles in the sliding window". What relation do you hope to discover?
But I think what people would really like to hear is the use case. Why do you need to do this? What is the background? Please give us some context as to why you need to do this. Is it homework, or some real world research into something (if so, what)? Knowing that, people may be able to suggest a better approach.
Answers (1)
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
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!