×
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

Using fnval in for loop SLOW

Using fnval in for loop SLOW

Using fnval in for loop SLOW

(OP)
Hello all,

I am writing a code for some data analysis, and I have to fit a spline to the local max and min points in data.  To fit the spline I am using

fun1 = csape(x1,y1)
fun2 = csape(x2,y2)

I then have to find the mean of the two splines. For that I use the following code.  (I did not want to use a for loop! I know this is not efficient coding in MATLab.)

for j=1:n
    m(j) = (funval(time(j),fun1) + funval(time(j),fun2))/2;
end

I originally tried this

m(1:n) = (funval(time(1:n),fun1) + funval(time(1:n),fun2))/2

MATLab did not like this.  Is there anyway to do this without running a for loop.  In some cases n > 20000, and I will have to repeat this 1000 times per case. And I might have 15 cases, and I don't want to have to wait two days for my results.  So I guess I am asking is there a way to do this without using a for loop, or is there a way that I can get my sline to be defined by an array and not a function?  Any ideas would be greatly appreciated.  The fnval takes a while on its own, put it in a for loop and it takes forever.

Regards,

jason m.

"Education is what remains after one has forgotten everything he learned in school."  Albert Einstein

RE: Using fnval in for loop SLOW

(OP)
I figured it out.  You can use fnval in the manner which I said could not be done.

ie..

m(1:n) = fnval(t(1:n),f1) + fnval(t(1:n),f2)

not sure what I was doing wrong before, but this does work, and my time to compute on cycle went from 2.4sec to .35sec.

Regards,

jason m.

"Education is what remains after one has forgotten everything he learned in school."  Albert Einstein

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