can this code be sped up?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I have a short program to take x-y-z (position position value) data and turn it into a matrix and then make a filled contour plot with it, i have large data sets (finished matrix is 1560 x 78). The data is listed with a fixed y for 1560 points with a changing x and z measurement, then that is repeated 77 more times with a different y. The code takes atleast 68 seconds to run depending on which computer i run it on. I was wondering if there is a way to change the code to speed it up. here is the code.
ifilename = uigetfile;
data=importdata(filename);
Matrixform=data.data;
x=Matrixform(:,1);
y=Matrixform(:,2);
z=Matrixform(:,3);
i=1;
m=length(unique(y));
n=length(unique(x));
yl=length(y);
for i=1:m;
j=1;
for j=1:n;
Mat(j,i)=z(j+n*(i-1));
end
end
[c,h] = contourf(Mat,200);
I have tried using meshgrid and griddata but the plot was smoothed too much ( i tried linear and cubic variations). i dont want to lose any detail from my data. thanks in advance
Answers (3)
Sean de Wolski
on 17 Jun 2011
Mat = zeros(n,m);
before the two for-loops will speed this up immensely for large [m n].
Also, you don't need to set j=1 in the first for-loop, this is done automatically on the initialization of the inner loop.
Robert Cumming
on 17 Jun 2011
you dont appear to initialize your Mat variable, i.e.
Mat = zeros(n,m)
This should make a big difference to start with
Andrew Newell
on 17 Jun 2011
You could replace the double loop by
Mat = reshape(z,n,m);
EDIT: I'll bet most of the time is spent calculating 200 contours. You could try
imagesc(Mat)
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!