add a row of summary statistics in a table

조회 수: 8 (최근 30일)
alpedhuez
alpedhuez 2022년 6월 18일
댓글: the cyclist 2022년 6월 18일
Suppose I have a table in Matlab:
Then I want to add a summary statistics (sum for the first col and average for the second col)
Then how shall I proceed?

채택된 답변

Voss
Voss 2022년 6월 18일
t = table({'new york';'chicago'},[10;5],[17;18],'VariableNames',{' ','visitor','temperature'})
t = 2×3 table
visitor temperature ____________ _______ ___________ {'new york'} 10 17 {'chicago' } 5 18
t(end+1,:) = {'' sum(t{:,'visitor'}) mean(t{:,'temperature'})}
t = 3×3 table
visitor temperature ____________ _______ ___________ {'new york'} 10 17 {'chicago' } 5 18 {0×0 char } 15 17.5

추가 답변 (1개)

the cyclist
the cyclist 2022년 6월 18일
Here is one way:
% The table
T = table([10;5],[17;18],'VariableNames',{'visitor','temperature'},'RowNames',{'New York','Chicago'});
% Numeric variables for the mean
vars = {'visitor','temperature'};
% Calculate the mean
varsMean = mean(T{:,vars});
% Append mean to table, and change the RowNames property of that row
T{end+1,vars} = varsMean;
T.Properties.RowNames{end} = 'mean';
% Display
T
T = 3×2 table
visitor temperature _______ ___________ New York 10 17 Chicago 5 18 mean 7.5 17.5
  댓글 수: 2
alpedhuez
alpedhuez 2022년 6월 18일
Thank you. But the first column is meant to be sum of elements.
the cyclist
the cyclist 2022년 6월 18일
Oh, sorry. I misread. Looks like you got an effectively equivalent answer.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by