Only required the mean value out rather then every single value in a data
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
muhammad choudhry
le 14 Juil 2022
Commenté : William Rose
le 21 Juil 2022
Hi,
I am taking the mean of every 7 rows in each column unitl the end of the column. I have used the code below but the problem is whenever I am reshaping the data I am getting everything. what I want is only the mean values saved in a different folder rather then mean value along with the values that used for mean. I used the mod function to add the value at the end of 7 values I think that's why its triggering whole data together while I only want to see the mean data. Table is 1639 by 182 after calculating the mean I am getting 1880 by 182. I want only the mean rows which is around 241 in this case. File is attached
Code:
close all; clear all; clc;
% Read the table
t = readtable('Required_Average.xlsx', ...
'NumHeaderLines',1, ...
'PreserveVariableNames',true);
data = table2array(t);
% Making sure total number of rows filled with nan if not the multiple of 7
% in case the total number of rows is not a multiple of 7,
% add rows of NaNs to fill it out:
[N,M] = size(data);
n = mod(N,7);
if n ~= 0
data = [data; NaN(7-n,M)];
N = N+7-n;
end
% reshape data to have size 7 in the third dimension
data = permute(reshape(data.',[M 7 N/7]),[3 1 2]);
size(data)
% calculate the mean in the third dimension
% and append it to the end of data
% (using 'omitnan' to prevent NaNs from affecting the mean)
data(:,:,end+1) = mean(data,3,'omitnan');
%Reshape the data
data = reshape(permute(data,[3 1 2]),[],M)
0 commentaires
Réponse acceptée
William Rose
le 14 Juil 2022
I assume your desired output has N rows by 182 columns, where N=floor(1639/7)=234.
num=xlsread('Required_Average.xlsx');
[r,c]=size(num);
N=floor(r/7);
a=zeros(N,c); %allocate array for the output
%row 1 of a() is mean of rows 1-7 of num()
%row 2 of a() is mean of rows 8-14 of num(), etc.
for i=1:N
a(i,:)=mean(num(7*i-6:7*i,:));
end
Try it. Good luck.
3 commentaires
Plus de réponses (1)
Peter Perkins
le 21 Juil 2022
"7" sounds rather suspiciously like "weeks". If this were a timetable, it would be a one-liner with groupsummary. Moral: don't throw away information.
Dunno what the dates would be, but like this:
>> t = readtable("Required_Average.xlsx"); t = t(1:10,1:3)
t =
10×3 table
RZ1 RZ2 RZ3
_____ _____ __________
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
12543 43424 3.2454e+05
>> tt = table2timetable(t,StartTime=datetime(2020,1,1),TimeStep=caldays(1))
tt =
10×3 timetable
Time RZ1 RZ2 RZ3
___________ _____ _____ __________
01-Jan-2020 12543 43424 3.2454e+05
02-Jan-2020 12543 43424 3.2454e+05
03-Jan-2020 12543 43424 3.2454e+05
04-Jan-2020 12543 43424 3.2454e+05
05-Jan-2020 12543 43424 3.2454e+05
06-Jan-2020 12543 43424 3.2454e+05
07-Jan-2020 12543 43424 3.2454e+05
08-Jan-2020 12543 43424 3.2454e+05
09-Jan-2020 12543 43424 3.2454e+05
10-Jan-2020 12543 43424 3.2454e+05
>> groupsummary(tt,"Time","week","mean")
ans =
2×5 table
week_Time GroupCount mean_RZ1 mean_RZ2 mean_RZ3
__________________________ __________ ________ ________ __________
[29-Dec-2019, 05-Jan-2020) 4 12543 43424 3.2454e+05
[05-Jan-2020, 12-Jan-2020) 6 12543 43424 3.2454e+05
Voir également
Catégories
En savoir plus sur Numeric Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!