Algorythm for Average of excel data
6 views (last 30 days)
Show older comments
Dear Sir please suggest .. How can I get average of my attached data in excel at an interval of every 5 datas. like at time interval 0-0.5 sec then 0.6-1 sec, 1-1.5 sec and so on....
1 Comment
Jon
on 21 Feb 2024
Edited: Jon
on 23 Feb 2024
You have a lot of what look like helpful answers to your question. Unless, there is something that has not been addressed in these answers, it would be good for you to now select one of them as the answer. This will allow the question to be marked as answered so that others will know that an answer is available.
Accepted Answer
Dyuman Joshi
on 15 Feb 2024
Edited: Dyuman Joshi
on 15 Feb 2024
data = readtable('S1IA.csv')
%define bins to distribute bins in
idx = 0:0.5:0.5*ceil(max(data.Time)/0.5);
%Get the mean of the rest of the columns for the specified bins
out = groupsummary(data, 1, idx, @mean, 'IncludedEdge', 'right')
3 Comments
More Answers (4)
Jon
on 15 Feb 2024
Edited: Jon
on 15 Feb 2024
If you have the Statistics and Machine learning toolbox you could do it like this
% Parameters
grpIncr = 0.5 % time increment for group averages
% Read the data into a matrix
dat = readmatrix('S1IA.csv')
% Provide grouping variable that makes elements within a specified sampling
% interval have the same group value
grp = floor(dat(:,1)/0.5);
[dat grp]
% Calculate mean of each group
stats = grpstats(dat(:,2:3),grp)
3 Comments
Voss
on 15 Feb 2024
Maybe something like this:
filename = 'S1IA.csv';
T = readtable(filename);
T.Time = seconds(T.Time);
T = table2timetable(T,'RowTimes','Time');
new_t = T.Time(1):seconds(0.5):T.Time(end);
T = retime(T,new_t,'mean')
0 Comments
Mathieu NOE
on 15 Feb 2024
hello again
well, this is quite the same as my answer to your other post
adapted to your new data file , this becomes :
data = readmatrix('S1IA.csv'); % Time,A,B
t = data(:,1);
dt = mean(diff(t));
%% home made solution (you choose the amount of overlap)
buffer_size = round(0.5/dt); % how many samples for 0.5 seconds buffer ?
overlap = 0; % overlap expressed in samples
%%%% main loop %%%%
[new_time,data_out] = my_movmean(t,data(:,2:3),buffer_size,overlap);
figure(2),
plot(t,data(:,2),new_time,data_out(:,1),'*-r');
title('A');
legend('raw data','0.5s mean');
xlabel('Time(s)');
figure(3),
plot(t,data(:,3),new_time,data_out(:,2),'*-r');
title('B');
legend('raw data','0.5s mean');
xlabel('Time(s)');
%%%%%%%%%% my functions %%%%%%%%%%%%%%
function [new_time,data_out] = my_movmean(t,data_in,buffer_size,overlap)
% NB : buffer size and overlap are integer numbers (samples)
% data (in , out) are 1D arrays (vectors)
shift = buffer_size-overlap; % nb of samples between 2 contiguous buffers
[samples,~] = size(data_in);
nb_of_loops = fix((samples-buffer_size)/shift +1);
for k=1:nb_of_loops
start_index = 1+(k-1)*shift;
stop_index = min(start_index+ buffer_size-1,samples);
x_index(k) = round((start_index+stop_index)/2);
data_out(k,:) = mean(data_in(start_index:stop_index,:),1,'omitnan'); %
end
new_time = t(x_index); % time values are computed at the center of the buffer
end
Alexander
on 15 Feb 2024
A very easy approach (as allways):
%Algorythm for Average of excel data
%https://de.mathworks.com/matlabcentral/answers/2082483-algorythm-for-average-of-excel-data
clear; close all;
data = dlmread('S1IA.csv',',',1,0);
t = data(:,1);A = data(:,2);B = data(:,3);
dy = floor(length(A)/5)
t = t(1:dy*5); % maximum 4 samples lost!
tr = reshape(t,5,dy);
trMean = mean(tr);
A = A(1:dy*5); % maximum 4 samples lost!
Ar = reshape(A,5,dy);
ArMean = mean(Ar);
B = B(1:dy*5); % maximum 4 samples lost!
Br = reshape(B,5,dy);
BrMean = mean(Br);
subplot(211)
plot(trMean ,ArMean); grid minor; title('A')
subplot(212)
plot(trMean, BrMean); grid minor; title('B')
@SATYA PAL beautifying is up to you.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!