Info

此问题已关闭。 请重新打开它进行编辑或回答。

can this code be sped up?

1 次查看(过去 30 天)
John
John 2011-6-17
关闭: MATLAB Answer Bot 2021-8-20
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

回答(3 个)

Sean de Wolski
Sean de Wolski 2011-6-17
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 2011-6-17
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 2011-6-17
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)

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by