Running calculations over columns

5 次查看(过去 30 天)
I currently have a running sample entropy code that works over a 300 sample point window. This works fine for matrices that contain only one column. I was wondering how I could amend the script in a way that it will calculate the running sample entropy for each column of a matrix with multiple columns. I'm guessing maybe a for loop may work in some way?
I have included the code with a sample example of data
X = randi(100,10000,1);
OutPut = SampEn(2,0.2,X,300);
function SampEnN = SampEn(dim,r,data,windowSize)
%%Set-up
correl = zeros(1,2);
[N,M] = size(data);
SampEnN = nan(N,M);
for t = windowSize+1:N
tolerance = std(data(t-windowSize:t, :),'omitnan')*r;
dataT = data(t-windowSize:t,:);
dataMat = zeros(dim+1,windowSize-dim);
for i = 1:dim+1
dataMat(i,:) = dataT(i:windowSize-dim+i-1);
end
for m = dim:dim+1
count = zeros(1,windowSize-dim);
tempMat = dataMat(1:m,:);
for i = 1:windowSize-m
% calculate Chebyshev distance, excluding self-matching case
dist = max(abs(tempMat(:,i+1:windowSize-dim) - repmat(tempMat(:,i),1,windowSize-dim-i)));
% calculate Heaviside function of the distance
% User can change it to any other function
% for modified sample entropy (mSampEn) calculation
D = (dist < tolerance);
count(i) = sum(D)/(windowSize-dim);
end
correl(m-dim+1) = sum(count)/(windowSize-dim);
end
C = log(correl(1)/correl(2));
SampEnN(t,:) = C;
end
end

采纳的回答

Naman Chaturvedi
Naman Chaturvedi 2018-8-17
Hi,
If you don't want to change the function, you can use the following for loop code to compute the desired output.
output=[];
for i=1:size(X,2)
output=[output SampEn(2,0.2,X(:,i),300)];
end
Hope this helps.
You can also modify(generalize) your function to calculate for any 2d array. MATLAB inbuilt functions work along columns and hence can generate column-wise results for a matrix.

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by