필터 지우기
필터 지우기

Average every 3 Rows in Single Column in Table

조회 수: 6 (최근 30일)
Spencer Ferris
Spencer Ferris 2021년 2월 21일
댓글: KALYAN ACHARJYA 2021년 2월 22일
I have a column in a table where I want to make a new column of data based on the average of every 3 values in that column. Tried some other solutions here but none seemed to work for me.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 21일
편집: KALYAN ACHARJYA 2021년 2월 21일
Lets consider one Table Example
LastName = {'A';'B';'C';'D';'E';'F'};
Age = [38;43;38;40;49;50];
T = table(LastName,Age);
Table
T =
6×2 table
LastName Age
_______ ___
{'A'} 38
{'B'} 43
{'C'} 38
{'D'} 40
{'E'} 49
{'F'} 50
Access the particular column, let's say age data
age_col=T.Age
Result:
>> age_col=T.Age
age_col =
38
43
38
40
49
50
Now Average the 3 consecutive rows of the column vector. Easiest way reshape the data then mean
data1=reshape(age_col,[3,2])
%........................^ you can generelize this sizes
Next find the mean and transpose
av_result=mean(data1)'
Result:
av_result =
39.6667
46.3333
  댓글 수: 2
Spencer Ferris
Spencer Ferris 2021년 2월 21일
편집: Spencer Ferris 2021년 2월 21일
Thanks so much! So this worked perfectly, only thing is my output for one of them was 2.307 when it should have been 2,307. Looks like it divided everything by 1000 for some reason?
Here's exactly what I did. It's giving me the correct values just everything is divided by 1000. I tried multiplying xcoord_divided by 1000 but that didn't seem to do anything.
EDIT: Just realized that it was just how matlab was displaying the values, all set and thank you!
xcoord = practice.x
xcoord_reshaped = reshape(xcoord,[3,length(xcoord)/3])
xcoord_divided= mean(xcoord_reshaped)'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by