在MATLAB中计算组均值/中位数,其中组ID在单独的列中 [英] Calculating group mean/medians in MATLAB where group ID is in a separate column
问题描述
我有一列包含每个参与者的组ID.一共有三组,因此此列中的每个数字都是1、2或3.
I have one column which contains the group ID of each participant. There are three groups so every number in this column is 1, 2 or 3.
然后我有第二列,其中包含每个参与者的响应分数.我想计算每组中的平均/中位数反应得分.
Then I have a second column which contains response scores for each participant. I want to calculate the mean/median response score within each group.
我设法通过遍历每一行来做到这一点,但是我感觉这是一个缓慢且次优的解决方案.有人可以建议一种更好的做事方法吗?
I have managed to do this by looping through every row but I sense this is a slow and suboptimal solution. Could someone please suggest a better way of doing things?
推荐答案
使用逻辑条件,例如说您的数据位于矩阵m
中,如下所示:第一个列为ID
,第二个列为响应得分,
Use logic conditions, for example say your data is in matrix m
as follows: the first col is ID
the second col is the response scores,
mean(m(m(:,1)==1,2))
median(m(m(:,1)==1,2))
将为您提供响应得分中1
的平均值和中位数,等等
will give you the mean and median for 1
in the response score, etc
这篇关于在MATLAB中计算组均值/中位数,其中组ID在单独的列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!