Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

own diff() function: Y= X2 - X1.....Xn+1-Xn

Status
Not open for further replies.

PsyDelic

Mechanical
Aug 19, 2006
2
Hello,

is there anybody who knows how to change the matlab included diff() function to the following form:

own diff() function: Y= X2 - X1.....Xn+1-Xn

I just have created an m File where I do this manually seperate from diff() like:

for(c=1:1:length(x)-1)
Y(c)=[(x(c)-x(c+1))];
end

The sample code above takes a lot of time. For my data file (including 1,750,000 values)it takes about 10 hours. The matlabs own diff takes a piece of this time. Hope you can understand my request in this matter.

Thanks in advance.


PsyDelic


 
Replies continue below

Recommended for you

As long as you have enough memory, I would think you can speed it up by doing it as a vector operation, rather than a loop.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Can you achieve it using ML's buitin, surrounded by a couple of flipud's? Otherwise it's MEX all the way.
 
You need to figure out how to use the built-in functions. They run hundreds of times faster than a explicit loop. The simplest solution is to create a reverse order index that you can use as one of the columns in your dataarray, sort on the column to reverse the order of the data and perform the usual diff.

TTFN



 
This has been cross-posted to CSSM. Kind of annoying really, choose your forum!
 
CSSM?

His profile only shows one posting to Eng-Tips

TTFN



 
Upon further investigation, the slowness of your process is mostly likely because you are dynamically allocating the arrays in the FOR loops.

As stated in the MATLAB help files preallocation significantly improves throughput.
Code:
dirsize = 2000000;

tic
A=zeros(dirsize,2);
Z=zeros(dirsize,1);
for i = 1:dirsize
  A(i,1)=dirsize+1-i;
  A(i,2)=i^2;
end
B=sortrows(A,1);
for i = 1:dirsize
  Z(i)=B(i,2);
end
C=diff(Z);
toc
t=toc

takes 2.4 seconds on a 1.8 GHz P4 with 512MB of memory.

The code creates a 2 column matrix, using the first column as a reverse index, data in the second column. The matrix is sorted, the second column is extracted and diff() is applied to the resultant vector.

TTFN



 
Thanks for your replies. I got a solution. I did a reverse Loop. I started with the last item of my array in the following way. It saved much more time. Now I need only a few secs. for it

for (c=length(x):1:1)
y=diff(x(c)-x(c+1));
end

Thank you very much for your help!

Bye
 
comp.soft-sys.matlab

This is the one-stop shop for Matlab duscussions - a USENET group that's been alive since c. 1993. All the experts reside there. Sometimes engineering questions stray here too. Othertimes there's the boring cross-posting activity.
 
SomtingGuy gave you the best answer. Did you try it? Your request and your example did not match. I assume you wanted something that achievedyour example program. Flipping the vector before and after the call to the built in diff will do exactly what you want, and most efficiently, as SmetingGuy stated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor