How can I optimize my Matlab function and bypass a lot str2double calls?

3 views (last 30 days)
Hello,
I have a Matlab function. This function will be called about 20.000 times with about 11.000 values at each call. So I do have a lot of calculations to do.
I do have some ideas how to optimize it, but my attempts doesn't work.
First: Here is the code:
function [ q ] = DataEstimation( x1,y1,z1,t )
path = 'C:\Path\To\Data'
mesh = 'C:\Path\To\Mesh'
persistent Nodes;
persistent times;
persistent names;
persistent Data;
if isempty(Nodes)
%write Nodes and names from Mesh and Data files
% This is how I use my Data dataset
Data.(char(strrep(['t' times{1,i}{1,1}],'.','_'))) = csvread(char(fullFileName), 0, 0);
end
i=1;
q=zeros(1,length(x));
tq=t(1);
while tq>str2double(times{1,i}{1,1})
i=i+1;
end
for k=1:length(x)
xq=x1(k);
yq=y1(k);
zq=z1(k);
for j = 1:length(Nodes)
if(and(and(xq==Nodes(j,1),yq==Nodes(j,2)),zq==Nodes(j,3)))
if tq~=str2double(times{1,i}{1,1})
t1=str2double(times{1,i-1}{1,1});
q1=Data.(char(strrep(['t' times{1,i-1}{1,1}],'.','_')))(j);
% ...
break
elseif(and((j==length(Nodes)),or(xq~=Nodes(j,1),or(yq~=Nodes(j,2),zq~=Nodes(j,3)))))
q = -1000 % Error Value
end
end
end
The whole code would be way too much, so I just postet the importent parts. I think there is a huge potential, when I wouldnt call 'str2double' and 'Data.(char(strrep(['t' times{1,i-1}{1,1}],'.','_')))(j)' Each time multiple times.
Can I somehow do this smarter without calling these functions each time?
Do you have any other suggestions?
Kind regards,
  5 Comments
dpb
dpb on 25 Jun 2018
Edited: dpb on 25 Jun 2018
How about telling us what it is you're really after and showing a small sample of the input data and expected result therefrom so folks here can see the actual problem instead of trying to micro-optimize another implementation?
Attach a small input file so folks have something to work with...
I don't understand why you don't just read the file as numeric instead from the git-go instead and vectorize what could be but without knowing what is attempted to be done from what that's tough to decipher.
Steven Lord
Steven Lord on 25 Jun 2018
I agree with dpb's suggestion. I think you're trying to determine which nodes are in your data set, but it's not completely clear. If I'm right, it might be as simple as calling ismember with the 'rows' flag. But I'm not certain I'm right. Explaining in words (not code) what you're trying to do may allow us to offer an alternate approach that avoids much or all of the conversion between text and numeric data.

Sign in to comment.

Answers (1)

Jan
Jan on 25 Jun 2018
Edited: Jan on 25 Jun 2018
Avoid repeated work by moving the conversion before the loop:
tmp = str2double(times{1,i}{1});
for k = 1:length(x)
xq = x1(k);
yq = y1(k);
zq = z1(k);
for j = 1:length(Nodes)
if xq == Nodes(j,1) && yq == Nodes(j,2)) && zq == Nodes(j,3)
if tq ~= tmp
t1 = str2double(times{1,i-1}{1});
q1 = Data.(char(strrep(['t' times{1,i-1}{1}],'.','_')))(j);
% ...
break
elseif j == length(Nodes) && ...
(xq ~= Nodes(j,1) || yq ~= Nodes(j,2) || zq ~= Nodes(j,3))
q = -1000 % Error Value
end
end
end
By the way: Do not use "path" as name of a variable. During debugging shadowing the built-in function with the same name can cause serious troubles.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!