在Matlab中,计算其中一列满足条件的一列的一部分的均值 [英] in matlab, calculate mean in a part of one column where another column satisfies a condition

查看:1163
本文介绍了在Matlab中,计算其中一列满足条件的一列的一部分的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Matlab还是很陌生,我很好奇该怎么做:

I'm quite new to matlab, and I'm curious how to do this:

我有一个相当大的(27000x11)矩阵,第8列包含一个数字,该数字有时会改变,但对于2000行(不一定是连续的)是恒定的.

I have a rather large (27000x11) matrix, and the 8th column contains a number which changes sometimes but is constant for like 2000 rows (not necessarily consecutive).

我想为第8列具有相同值的那些行计算第3列中条目的平均值.对于第8列的每个值. 我还想将第3列的均值绘制为第8列的值的函数,但是如果我可以得到一个包含[mean_of_3rd,8th]的新矩阵(2x2),则可以这样做.

I would like to calculate the mean of the entries in the 3rd column for those rows where the 8th column has the same value. This for each value of the 8th column. I would also like to plot the 3rd column's means as a function of the 8th column's value but that I can do if I can get a new matrix (2x2) containing [mean_of_3rd,8th].

Ex :(为方便起见,较小的矩阵)

1 2 3 4 5
3 7 5 3 2
1 3 2 5 3
4 5 7 5 8
2 4 7 4 4

Ex: (smaller matrix for convenience)

1 2 3 4 5
3 7 5 3 2
1 3 2 5 3
4 5 7 5 8
2 4 7 4 4

由于第4列在第1行和第5行中具有相同的值,我想计算2和4的均值(第2列的相应元素,斜体粗体),并将其与第4列的平均值一起放在另一个矩阵中价值. 3和5(粗体)相同,因为第四列的两个值相同.

Since the 4th column has the same value in row 1 and 5 I'd like to calculate the mean of 2 and 4 (the corresponding elements of column 2, italic bold) and put it in another matrix together with the 4th column's value. The same for 3 and 5 (bold) since the 4th column has the same value for these two.

3 4
4 5

3 4
4 5

以此类推...这可能以简单的方式实现吗?

and so on... is this possible in an easy way?

推荐答案

使用功能强大的,未充分利用的accumarray:

Use the all-mighty, underused accumarray :

此行为您提供第二列累加的第四列平均值:

This line gives you mean values of 4th column accumulated by 2nd column:

means = accumarray( A(:,4) ,A(:,2),[],@mean)

此行为您提供每个集合中的元素数量:

This line gives you number of element in each set:

count = accumarray( A(:,4) ,ones(size(A(:,4))))

现在,如果您只想过滤那些至少发生过一次的事件:

Now if you want to filter only those that have at least one occurence:

>> filtered = means(count>1)

filtered =

     3
     4

这仅适用于第4列中的正整数.

This will work only for positive integers in the 4th column.

计数每组元素数量的另一种可能性:

Another possibility for counting amount of elements in each set:

 count = accumarray( A(:,4) ,A(:,4),[],@numel)

这篇关于在Matlab中,计算其中一列满足条件的一列的一部分的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆