"leo nidas" <bleonidas25@yahoo.gr> wrote in message <idm4en$6p7$1@fred.mathworks.com>...
>
> Hi there,
>
> I made a function that calculates the value of the cumulative hazard of some data at specific points requested by the user. Nothing special (see below). I would like to get rid of the for function if it is possible and speed things up.
>
> (I am going to run the following subroutine on a simulation, and repeat thousands of times, that's why I need some speed up)
>
> Thanx in advance for any answers!!
>
>
> function H=kaplan(x,status,at);
>
> %Calculates the Hazard function at a specific time "at".
> %Estimation yielded is from the Kaplan and Meier estimator.
>
> %x: time to censoring or to event
> %status: binary : 1=censored, 0=death
> %at: value of the cumulative hazard function at time "at"
>
> %("at" can be a vector)
> % but if the "at" provided is like at=0:0.01:10 it takes very long due to
> % the for loop...
>
> xx=x;
> for i=1:length(at)
>
> [y x]=ecdf(xx,'censoring',status,'function','cumulative hazard');
> if isempty(find(x<at(i)))==1
> H(i)=0;
> else
> H(i)=max(y(find(x<at(i))));
> end
> end
> H=H';
This line:
[y x]=ecdf(xx,'censoring',status,'function','cumulative hazard');
is not dependent on i, so pull it out of the loop.
preallocate H, since you know it's final size
H = zeros(length(at),1);
These will speed it up quite a bit.
For a complete vectorization try this and see if it works
[y x]=ecdf(xx,'censoring',status,'function','cumulative hazard');
H = zeros(length(at),1);
H(x<at) = max(y(x<at));
