File Exchange

image thumbnail

Sample Entropy

version (1.73 KB) by Kijoon Lee
This short code calculates the sample entropy (SampEn) of a given time series data.


Updated 21 Mar 2012

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

Hi, the vectorized version of the algorithm is "almost" awesome. This code is missing matches between templates. The creation of the 'dataMat' matrix is incomplete. Send me an e-mail if you want a clearer explanison. Thanks!

hello every one, please tell me what value of embedded dimension (dim) should be chosen ?


Hi, I am using this function to compute the complexity of the EEG signals. My EEG signal has 14 channels (electrodes) and approximately 15000 points at each channel. I run this code separately for each channel. but I am wondered about the stationarity criteria of the entropy measurements. Because for the measurement of entropy the data should be stationary. when we use this code for EEG signal, how we meet the stationarity requirement for entropy measurement? any help in this regard will be appreciated. Thank you

Anna French

This method is very fast compared to code written by one of my collaborator (could probably be sped up by using bsxfun instead of repmat, but that is beside the point).

This code is correct, at least by the method defined in pubmed article #10843903. The commenters that say it is incorrect either did not understand the code, are confusing this method with other sample entropy methods (ie approximated methods), or are mistaken. They are not just "computing it with the last part of the data", they are just selecting the columns so as to not duplicate calculations that have already been done.

Also the N-m-1 issue brought up is not important to the overall calculation, since those division values cancel anyhow (the writer actually didn't need to include them at all in their code).

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 Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Fast Approximate Entropy

Inspired: Fast sample entropy, Sample Entropy

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor