# Only required the mean value out rather then every single value in a data

2 ビュー (過去 30 日間)
muhammad choudhry 2022 年 7 月 14 日
コメント済み: William Rose 2022 年 7 月 21 日
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;
'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)

サインインしてコメントする。

### 採用された回答

William Rose 2022 年 7 月 14 日
I assume your desired output has N rows by 182 columns, where N=floor(1639/7)=234.
[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 件のコメント表示非表示 2 件の古いコメント
William Rose 2022 年 7 月 15 日

サインインしてコメントする。

### その他の回答 (1 件)

Peter Perkins 2022 年 7 月 21 日
"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
##### 1 件のコメント表示非表示 なし
William Rose 2022 年 7 月 21 日

サインインしてコメントする。

### カテゴリ

Find more on Tables 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!

Translated by