How to take an average of 7 rows of all the column and save into the following row and next 7 until the data finisih?

1 次查看(过去 30 天)
Hi,
I want to take the average of 7 rows of each column and save into next row then next 7 until the data finish in the file attached. can anyone help.

采纳的回答

Voss
Voss 2022-7-13

更多回答(2 个)

Vijeta Singh Yadav
Vijeta Singh Yadav 2022-7-13
编辑:Vijeta Singh Yadav 2022-7-13
arr=readmatrix("L:\data.csv",NumHeaderLines=9)
rows=size(arr,1)
for idx=1:7:rows
a=arr(idx:idx+6,:)
avg=mean(a,1)
arr(rows+idx,:)=avg;
end
Visit following links for more clarity on the functions used:

Harshit Gupta
Harshit Gupta 2022-7-13
Hi, I understand that you want to create an average vector where each element is the average of all elements in consecutive 7 rows.
What you can do is reshape things so the matrix is 7 x n x m.
Each cell of this matrix will now consist of 7 numbers in a column that you want to average.
Then you would take the mean of each column of that matrix. The result would be 1 x n x m, and we will have averaged the 7 elements in each column together that you wanted averaged. Now we can take average of each row and that would create the desired result that you want
Here's an example:
M = [1:2401];
M = reshape(M, 49, 49); % Dummy matrix of 49 x 49 dimensions
% Now we reshape the matrix into 7 x 7 x 49, and use the squeeze function
% to get back a 7 x 49 matrix, which contains average of 7 consecutive
% numbers in a column
B = squeeze(mean(reshape(M,[7,7,49]),1))
B = 7×49
4 53 102 151 200 249 298 347 396 445 494 543 592 641 690 739 788 837 886 935 984 1033 1082 1131 1180 1229 1278 1327 1376 1425 11 60 109 158 207 256 305 354 403 452 501 550 599 648 697 746 795 844 893 942 991 1040 1089 1138 1187 1236 1285 1334 1383 1432 18 67 116 165 214 263 312 361 410 459 508 557 606 655 704 753 802 851 900 949 998 1047 1096 1145 1194 1243 1292 1341 1390 1439 25 74 123 172 221 270 319 368 417 466 515 564 613 662 711 760 809 858 907 956 1005 1054 1103 1152 1201 1250 1299 1348 1397 1446 32 81 130 179 228 277 326 375 424 473 522 571 620 669 718 767 816 865 914 963 1012 1061 1110 1159 1208 1257 1306 1355 1404 1453 39 88 137 186 235 284 333 382 431 480 529 578 627 676 725 774 823 872 921 970 1019 1068 1117 1166 1215 1264 1313 1362 1411 1460 46 95 144 193 242 291 340 389 438 487 536 585 634 683 732 781 830 879 928 977 1026 1075 1124 1173 1222 1271 1320 1369 1418 1467
% Now we take average of each row
C = mean(B')
C = 1×7
1180 1187 1194 1201 1208 1215 1222
I've haven't used data from your excel sheet but if you are able to read the data from your excel sheet and make a matrix like the one used above, then this should work.
Refer to these documentation links to learn more about the functions used here: squeeze, reshape, mean
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by