File Exchange

image thumbnail

Sample Entropy

version 1.0 (1.73 KB) by

This short code calculates the sample entropy (SampEn) of a given time series data.

17 Ratings



View License

SampEn is a measure of complexity that can be easily applied to any type of time series data, including physiological data such as heart rate variability and EEG data.

SampEn is conceptually similar to approximate entropy (ApEn), but has following differences:

1) SampEn does not count self-matching. The possible trouble of having log(0) is avoided by taking logarithm at the latest step.
2) SampEn does not depend on the datasize as much as ApEn does. The comparison is shown in the graph above. This property makes it amenable to applications with relatively short data size.

This code uses the same vectorisation technique as in Fast Approximate Entropy, another submission from the same author.

Comments and Ratings (26)

Omer Çel


Victor (view profile)

This function doesn't compute correctly the sample entropy.
In line 39 ( dist = max(abs(tempMat(:,i+1:N-dim) - repmat(tempMat(:,i),1,N-dim-i))); ), distance is compute only with the last part of the data, instead of whole data less self-matching). Also, the denominator in " count(i) = sum(D)/(N-dim); " (line 45), is N - dim - 1

Another user has also implemented sample entropy:

Its code has a small bug but only a small change is required. See my comment in there.

I found this site that explains more about it in case anyone was interested:

Ares Christ

Kainan WANG

Hi! Can you comment from which article/book/whatever you extracted this code?



mejali (view profile)

How would I best modify this function to calculate SampEn as a sliding window? I.e. I have a 1000 data points, but I would like it to calculate SampEn for the first 200 beat intervals, then the next 200 intervals starting from 2:201, then 3:202..and so on. I tried some ways but failed each time. Appreciate your help.


Dear sir,
How can this be applied on the Image.

Thank You


Jonne (view profile)

When an embedded dimension of 1 is used, the code doesn't work. This is due to the 'max' function called in line 39. With an embedded dimension of 1, there is no need to look for the maximum absolute differences. Use the following code instead of line 39:

if m==1;
dist = abs(bsxfun(@minus,tempMat(:,i+1:N- dim),tempMat(:,i)));
dist = max(abs(bsxfun(@minus,tempMat(:,i+1:N-dim),tempMat(:,i))));

To make the function a little faster, I suggest to replace line 39 from:
dist = max(abs(tempMat(:,i+1:N-dim) - repmat(tempMat(:,i),1,N-dim-i)));


dist = max(abs(bsxfun(@minus,tempMat(:,i+1:N-dim),tempMat(:,i))));


Mo (view profile)

I am new to this field and I am struggling to understand your code according to function for Sample Entropy:
saen = SampEn( dim, r, data, tau )

Could you please explain in following codes what is the meaning of different variables (e.g. ‘nargin<4’; ‘datamat’; ‘zeros’; ‘correl’)?
Where is ‘r’ value in this code?
How we can change ‘dim’ value in this code?
What does mean correl =zeros(1,2)?

I am trying to use this matlab code for step variability in gait and my ‘m’ would be 2 and ‘r’ would be between 0.1-0.25 std. How can I amend this code for my analysis?

I really appreciate for any helps.


S (view profile)


Hesam (view profile)

I wrote .c file which is more than two times faster than .m file.

zhou wei

Liang ZOU

miss khatimah


Regard my previous one star rating as a mistake. Apparently mathworks immediately submitted the rating when I clicked on one of the stars.




May I know what is the first argument dim in the function? I want to pass on-channel EEG signal 500 samples (with sampling frequency 128 Hz) to the program to calculate entropy? Also please advice me how to use 'r'?


Hugh (view profile)

oops... I found that function rand is uniform distribution, not white noise. Your code is right!


Hugh (view profile)

I have a question.
It is known that if the input data is white noise, SampEn will decrease as tau increases. However, I tried this code with input rand(1,2000) and different tau, and it seems that SampEn will have similar values no matter what tau. What's wrong with this?

MATLAB Release
MATLAB 7.13 (R2011b)

Inspired by: Fast Approximate Entropy

Inspired: Sample Entropy, Fast sample entropy

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video