How can i speed up my function?

Hello everybody,
I'm trying to increase the performance of my Matlab function. Input is a table with the same columns as airfoil_update.
The function splits the X-Coordinates of one AeroDataId at the minimum of X/C and does this for every AeroDataId in the table. Airfoil, the input table, will have a size of 131123421x4. But the fourth column "Upper" is empty and should be filled up with a 1, if the coordinate belongs to the upper airfoil and 0, if it belongs to the lower airfoil.
Thank you very much in advance.
function [airfoil_update] = splitAirfoil(airfoil)
%% create table for upper and lower airfoil
airfoil_update=array2table(zeros(0,4),'VariableNames',{'AeroDataId','X_C','CP','Upper'});
%% vector with unique AeroDataIds
DataId_unique=unique(airfoil.AeroDataId);
for i=1:1:size(DataId_unique)
%% temp vector filled with every point of specific AeroDataId
disp(DataId_unique(i,:));
AeroDataId=airfoil(airfoil.AeroDataId==DataId_unique(i,:),:);
%% find the minimum of X/C
[~, min_idx]=min(AeroDataId.X_C);
%% every point with same ID before minimum -> lower_airfoil -> Upper =0
airfoil_new=AeroDataId(1:min_idx,:);
airfoil_new.Upper(:,:)=0;
airfoil_update=[airfoil_update; airfoil_new];
%% every point with same ID after minimum = upper_airfoil -> Upper =1
airfoil_new=AeroDataId(min_idx+1:end,:);
airfoil_new.Upper(:,:)=1;
%% add new rows
airfoil_update=[airfoil_update; airfoil_new];
end
end

4 Comments

Well,
Besides avoid repeated lines, wicht I think you didn't , you can make your vectors and matrixes sparse, sympley like this:
sparse(airfoil_update);
sparse(airfoil_new);
and goes on...
What will do is basically squeeze out all the zeros in the matrixes and vectors
Thanks.
You see repeated lines?
And if there are zeros in the vectors or tables, I need them.
They wont be deleted.. Its only for the matter of performance.. It will lower your program computacional time
Gerrit
Gerrit on 9 Jan 2020
Edited: Gerrit on 9 Jan 2020
Oh okay I understand. But where would I put it?
And I just saw sparse does not work for tables.

Sign in to comment.

 Accepted Answer

Philippe Lebel
Philippe Lebel on 8 Jan 2020
Edited: Philippe Lebel on 8 Jan 2020
In order to help you in a significant way, it would be useful to have a sample data set on which we could run the function.
On the top of my head, i'd remove the disp() function call. Printing info to the console takes a significant amount of time for no real benefit.
I would also pre-alocate the size of
airfoil_update
since you know it's maximum size, you can pre alocate it to that size and keep track of the number of elements you put in it then chop the unused space only once at the end of the script.

9 Comments

How can I give it to you? Can I upload a .csv file?
Yeah, I also thought about this, but than I need another algorithm so the rows are not added after all the zeros. Can you help me there?
you can attach a file to your question as you would do in a emal
tic
airfoil=readtable('MsesData_0-500.csv','ReadVariableNames',1);
toc
tic
airfoil_update=splitAirfoil(airfoil);
toc
here is my shot, the bigger part of the computation time was copying the data, so i avoided doing that.
function [airfoil] = splitAirfoil_new(airfoil)
%% vector with unique AeroDataIds
[DataId_unique,ia, ic]=unique(airfoil.AeroDataId);
ia = [ia; size(airfoil.AeroDataId,1)];
min_indexes = [];
nrow = size(airfoil,1);
airfoil.Upper = zeros(nrow, 1);
for i=1:1:size(DataId_unique)
%% find the minimum of X/C
[~, min_idx]=min(airfoil{ia(i):ia(i+1)-1,2});
airfoil.Upper(ia(i)+min_idx:ia(i+1)-1) = 1;
end
end
in the end, since matlab r2011, the automatic growth of arrays is handled pretty well, so there is not a big chunk of processing time to be gained there anyway.
The code you provided yielded and empty airfoil.Upper (Empty matrix: 122859-by-0). I'm not sure why. So watch out because my result is not exctly the same form factor as yours.
clear
airfoil=readtable('MsesData_0-500.csv','ReadVariableNames',1);
tic
airfoil_update_1=splitAirfoil_new(airfoil);
toc
tic
airfoil_update_2=splitAirfoil_init(airfoil);
toc
Elapsed time is 0.151890 seconds.
Elapsed time is 1.499844 seconds.
(i removed the 'disp()' part of your function to compare)
Wow! This is crazy! Thank you very much. Maybe you could help me with another function ?
Yeah I already corrected it, so it doesnt load this empty matrix anymore.
could take a look at it, post an other question and comment the link to the new question here
I just edited my answer, there was a lot of unused code and the second for loop was not needed.
Thank you veryyy much Philippe!!
here is the link: https://de.mathworks.com/matlabcentral/answers/499639-how-can-i-speed-up-my-code
Philippe, do you have any idea for my other problem?

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Asked:

on 8 Jan 2020

Commented:

on 12 Jan 2020

Community Treasure Hunt

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

Start Hunting!