functions instead eval?

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 Commenti

Image Analyst
Image Analyst il 26 Mag 2020
Modificato: Image Analyst il 26 Mag 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.
eng had
eng had il 26 Mag 2020
Modificato: eng had il 26 Mag 2020
WELL!! thank you for your response BUT, my question is NOT as stupid as you think!!! it was Specific and brief.
this line of code and other thousdend similar equations is repeated thousand times on hundreds of 157*157 images (that is my project)
SO my code last more than 12 hours !!!!!!!!!!!!!
I used parfor , but EVAL() not working with parfor
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
Stephen23
Stephen23 il 26 Mag 2020
Modificato: Stephen23 il 26 Mag 2020
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
Mohammad Sami il 26 Mag 2020
Modificato: Mohammad Sami il 26 Mag 2020
Is the entire equation randomly generated, or only the data used in equation is randomly generated.
eng had
eng had il 26 Mag 2020
Modificato: eng had il 26 Mag 2020
the entire equation (with math operations) is generated randomly.
yes the MATLAB will parse the genrated code, and eval is good but I'm asking if there is another way, that can improve the speed of my code , because the main process is genrate a random codes and evalute it on the image dataset.
How can I convert my string to an equation and solve it, with using variables as indexing array element ?
Mohammad Sami
Mohammad Sami il 26 Mag 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
Bjorn Gustavsson il 26 Mag 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
eng had il 26 Mag 2020
the purpose is to find a new feature extraction method, to get features from image
Rik
Rik il 26 Mag 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
eng had il 26 Mag 2020
Yes I KNOW, but that is called a RESEARCH
Rik
Rik il 26 Mag 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
eng had il 26 Mag 2020
And "This may sound stupid" is not insult . thank you .
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...
eng had
eng had il 26 Mag 2020
Modificato: eng had il 26 Mag 2020
yes, I need to find an expression that contain {-,+,*,/} , random numbers(const), and the values of a sliding window along the image . to detect the relation between the pixles in the sliding window .
So the operand are generated randomly , there is no rule for the generation. My code depends on generation this expression through a for loop ,
Expres=rand();
for j =1: ExpLength
PixIndex = ['mask(' num2str(randi([1,25])) ')'];
op= randi([1,4]);
if (op == 1)
op='+';
elseif (op == 2)
op='-';
elseif (op == 3)
op = '*';
else op = '/';
end
a=[op,PixIndex];
Expres =[Expres a];
end
Bjorn Gustavsson
Bjorn Gustavsson il 26 Mag 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
per isakson il 27 Mag 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
Image Analyst il 27 Mag 2020
Modificato: Image Analyst il 27 Mag 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.

Accedi per commentare.

Risposte (1)

rubindan
rubindan il 27 Mag 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

Richiesto:

il 26 Mag 2020

Risposto:

il 27 Mag 2020

Community Treasure Hunt

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

Start Hunting!

Translated by