Azzera filtri
Azzera filtri

Quicker alternative to EVAL

8 visualizzazioni (ultimi 30 giorni)
Mike
Mike il 11 Apr 2012
Hi,
I was wondering if anyone could suggest a quicker way of solving this problem.
I have a function that generates an 'n x n' cell array (A) where each cell contains a string. These strings are equations for calculating variables. For example, if n=3, A =
'theta(1).*theta(1)' 'theta(1).*theta(2)' 'theta(1).*theta(3)'
'theta(2).*theta(1)' 'theta(2).*theta(2)' 'theta(2).*theta(3)'
'theta(3).*theta(1)' 'theta(3).*theta(2)' 'theta(3).*theta(3)'
where theta is a predefined variable.
I have tried the 'eval' function in a loop for this and it works.
for i = 1:n
for j = 1:n
a(i,j)=eval(A{i,j});
end
end
However, this code is used in an optimization and I have found the optimizer is much quicker if I manually copy and paste each string and type within the .m file:
a11 = theta(1).*theta(1)
a12 = theta(1).*theta(2)
a13 = ...
The problem with this approach is that I would like to automate the code so that 'n' can be increased to any value so doing it manually would not be pratical for high values.
Is there a way to get matlab to write these strings into an m file once they've been generated? Sort of 'copying and pasting' but getting matlab to do the hard work! I've been looking but can't seem to find one.
Any feedback anyone could give would be greatly appreciated.
Thanks,
Mike

Risposta accettata

Jan
Jan il 11 Apr 2012
Avoid the usage of eval when you need a fast program, and in all other cases also.
Instead of creating the strings 'theta(1).*theta(1)' it would be smarter to store the indices only:
A = {[1,1], [1,2], [1,3]; ...
[2,1], [2,2], [2,3]; ...
[3,1], [3,2], 3,3]};
for i = 1:n
for j = 1:n
index = A{i, j};
a(i,j) = theta(index(1)) * theta(index(2));
end
end
Using a numerical 3D array might be even faster.
Creating files dynamically has the big disadvantage, that reading and parsing the M-files is very time-consuming. Matlab is much more efficient, if you use static M-files and vary the parameters only.
  1 Commento
Daniel Shub
Daniel Shub il 11 Apr 2012
Even if theta is also changing, it you save all your possible variables in a structure, then you can extend Jan's solution to include more information while still avoiding eval.

Accedi per commentare.

Più risposte (1)

Jacob Halbrooks
Jacob Halbrooks il 11 Apr 2012
I would suggest trying a few approaches and seeing which is fastest with TIC/TOC. For more advanced performance debugging, use MATLAB Profiler.
If you want to automate your copy and paste of the strings, use FPRINTF to generate your script, which you can then run:
fid = fopen('myscript.m','wt');
n = 3;
for i = 1:n
for j = 1:n
fprintf(fid,'A%u%u = %s;\n', i,j,A{i,j});
end
end
fclose(fid);
run myscript;
  1 Commento
Mike
Mike il 11 Apr 2012
This is brilliant, thank you. It's exactly what I was trying to do. I've tested the timing against the other two methods and it's not a fast as when all the equations are copied and pasted into a script but it's an order of magnitude faster than eval! Thanks again.

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by