Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
speed up, vectorize, find, ecdf, hazard

Subject: speed up, vectorize, find, ecdf, hazard

From: leo nidas

Date: 7 Dec, 2010 20:12:07

Message: 1 of 2


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';

Subject: speed up, vectorize, find, ecdf, hazard

From: Sean de

Date: 7 Dec, 2010 21:08:07

Message: 2 of 2

"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));

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us