can this code be sped up?

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

Réponses (3)

Sean de Wolski
Sean de Wolski le 17 Juin 2011

0 votes

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
Robert Cumming le 17 Juin 2011

0 votes

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
Andrew Newell le 17 Juin 2011

0 votes

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)

Cette question est clôturée.

Produits

Question posée :

le 17 Juin 2011

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by