How can I calculate average from specific rows to another rows in a datatable?

Dear community
I'm trying to design a script, in order to calculate the average from the 1st to 3rd row, then from the 4th to 6th and finally from the 7th to 9th row. The data has this structure:
Comment Cell Temperature
24-04-2019 A8 38,76736069
24-04-2019 A8 38,70141602
24-04-2019 A8 39,03113937
24-04-2019 C5 38,56953049
24-04-2019 C5 39,29491806
24-04-2019 C5 39,26194382
24-04-2019 E1 36,5252533
24-04-2019 E1 36,6241684
24-04-2019 E1 36,36038971
The temperature are in the 6th column. These data are like datatable. I have this script:
Clean=table2cell(datatable);
nrow = height(datatable);
lastidx = floor(nrow/3) * 3;
T_p = mean(reshape(datatable{1:lastidx,6}, 3, []));
T_C=T_p.';
But doesn´t work. How can I do?
Any for loop?
Help please.
Thank U

6 Commenti

Looks ok on surface; what does "doesn't work" mean? Error (if so, give us the error text in context with all red text pasted) or what???
Doesn’t show an matlab error. When I calculate the average of each group of Measurements in excel, the matlab results don’t match with the first ones.
Well, we can't say anything about numbers w/o data to look at. Maybe Excel isn't the same set, who knows??? Certainly the general idea of reshape() in memory storage order works as can be easily demonstrated...for a local variable D from your data above--
>> mean(reshape(D,3,[])).'
ans =
38.8333
39.0421
36.5033
>> mean([D(1:3) D(4:6) D(7:9)])
ans =
38.8333 39.0421 36.5033
>>
It’s the same data. Previously I import all Excel files as datatable and the idea is matlab calculate the average of the three groups of Measurements as explain before.
When I apply the last commands appears this error
mean(reshape(Clean,3,[])).'
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
Error in Untitled2 (line 23)
mean(reshape(Clean,3,[])).'
mean([Clean(1:3) Clean(4:6) Clean(7:9)])
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
Error in Untitled2 (line 23)
T=mean([Clean(1:3) Clean(4:6) Clean(7:9)])
I have 35 files with the structure I wrote at the beginning
I dunno what to do
@dpb I already fixed it.
Thanks anyway =)

Accedi per commentare.

Risposte (0)

Richiesto:

il 14 Giu 2019

Commentato:

il 14 Giu 2019

Community Treasure Hunt

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

Start Hunting!

Translated by