MATLAB Answers

interpolation of data takes minutes to operate

2 views (last 30 days)
Abdulkarim Almukdad
Abdulkarim Almukdad on 27 Oct 2020
Commented: Mathieu NOE on 27 Oct 2020
I have a data of 5068x59 and I want to interpolate each column with respect to column number 4. I have used the below code and its working fine. However, it takes sometime to operate. Therefore, is there any easier way to perform the below tasks
T=readmatrix('1.xlsx');
x = T(:,4) ;
for b=1:width(T)
for bb=1: length(x)
T1(bb,b)=T(bb,b)+ bb*1E-11; %to make all values unique
end
end
distanceQ = 1:max(T1(:,4)); % new distance vector with step size of 1m
for cc=1:width(T1)
speedQ = interp1(T1(:,4),T1(:,cc) , distanceQ);
filename = 'x)Data.xlsx';
writematrix(speedQ,filename,'WriteMode','append')
end

  5 Comments

Show 2 older comments
Mathieu NOE
Mathieu NOE on 27 Oct 2020
hi
First, you can avoid the first two nested loops . This is fairly simple to do (dummy example) :
% dummy data
T = [1;1;1;1;1;1;2;3;4;5;5]*[1 2 3 4]
%%%%% main code %%%%%%%%%%
[m,n] = size(T);
offset = 1e-4*((1:m)'*ones(1,n));
T1 = T+offset
T =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
5 10 15 20
5 10 15 20
T1 =
1.0001 2.0001 3.0001 4.0001
1.0002 2.0002 3.0002 4.0002
1.0003 2.0003 3.0003 4.0003
1.0004 2.0004 3.0004 4.0004
1.0005 2.0005 3.0005 4.0005
1.0006 2.0006 3.0006 4.0006
2.0007 4.0007 6.0007 8.0007
3.0008 6.0008 9.0008 12.0008
4.0009 8.0009 12.0009 16.0009
5.0010 10.0010 15.0010 20.0010
5.0011 10.0011 15.0011 20.0011
Next , I am not sure if the "append" method to save your data in the excel file is taking long.
I would prefer to store all results first in an array or cell and then save it once.
is "speedQ" a vector of constant lenght ? if yes, there might be a faster solution.
You can use the profiler to see the bottlenecks in your code.
Abdulkarim Almukdad
Abdulkarim Almukdad on 27 Oct 2020
Thanks a lot, that was helpfull. firstly I have changed the loop to the offest function and it works great, as for your what takes most of the time is indeed the writefile function so I have followed what you mentioned to store all the data in 1 variable then to save that variable once as shown below. as for your question, my code is intending to read several files and not all of the files have the same length of data but the difference is just between 1-5 columns. I just have 1 simple questions and I will be very thankfull if you can help me with it. how can I make the result from the interpolation appear in 1 column rather than in 1 row. without using additional transpose functions if possible.
T=readmatrix('1.xlsx');
x = T(:,4) ;
[m,n] = size(T);
offset = 1e-11*((1:m)'*ones(1,n));
T2 = T+offset;
distanceQ = 1:max(T2(:,4)); % new distance vector with step size of 1m
for cc=1:width(T2)
speedQ(cc,:)= interp1(T2(:,4),T2(:,cc) , distanceQ);
end
filename = 'xxx111)Data.xlsx';
writematrix(speedQ,filename,'WriteMode','append')
Mathieu NOE
Mathieu NOE on 27 Oct 2020
hi
glad it helped you
I believe the ouput of the interp1 function is a row vector because distanceQ must also be a row vector
you must change the orientation of distanceQ , by putting distanceQ(:) instead of distanceQ in the interp1 function call
so this should give the correct orientation for speedQ:
speedQ(:,cc)= interp1(T2(:,4),T2(:,cc) , distanceQ(:));

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by