×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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


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

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.

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

Can you achieve it using ML's buitin, surrounded by a couple of flipud's?  Otherwise it's MEX all the way.

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

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



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

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

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

CSSM?

His profile only shows one posting to Eng-Tips

TTFN



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

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



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

(OP)
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

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

Quote:

CSSM?
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.

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

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.

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

Actually Duane Hanselman gave the best answer in CSSM:

-diff(x)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources