MATLAB查找并将函数应用于重复索引的值 [英] MATLAB find and apply function to values of repeated indices

查看:143
本文介绍了MATLAB查找并将函数应用于重复索引的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个352x11矩阵,由具有10个数据点的列1索引.某些索引值被重复.我想找到重复的索引并计算重复试验的平均数据点(如果可能的话,避免循环).

I have a 352x11 matrix, indexed by column 1 with 10 data points. Some of the index values are repeated. I'd like to find the repeated indices and calculate the mean data points for the repeated trials (avoiding loops, if possible).

例如,

x =

   26   77.5700   17.9735   32.7200
   27   40.5887   16.6100   31.5800
   28   60.4734   18.5397   33.6200
   28   35.6484   27.2000   54.8000
   29   95.3448   19.0000   37.7300
   30   82.7273   30.4394   39.1400

最后以:

ans =

   26   77.5700   17.9735   32.7200
   27   40.5887   16.6100   31.5800
   28   48.0609   22.8699   44.2150
   29   95.3448   19.0000   37.7300
   30   82.7273   30.4394   39.1400

我在考虑是否要使用

J = find(diff(x(:,1))==0);

要找到重复值的位置,我可以将函数应用于x的相应位置,但是我应该从哪里开始?

to find the position of the repeated values, I could then apply the function to the corresponding positions of x, but where do I begin?

推荐答案

您可以将accumarray应用于多个列

You can apply accumarray to multiple columns as shown here

labels = x(:,1) - min(x(:, 1)) + 1; 
labels = [repmat(labels(:),size(x,2),1), kron(1:size(x,2),ones(1,numel(labels))).'];             
totals = accumarray(labels,x(:),[], @mean);

这是根据 Gnovice的代码改编的.

要使其适用于您的代码,您需要删除前面的所有零

To get it to work for your code you then need to delete all the zeros in the front

totals(find(mean((totals == zeros(size(totals)))')), :) = [];

这将产生所需的

   26.0000   77.5700   17.9735   32.7200
   27.0000   40.5887   16.6100   31.5800
   28.0000   48.0609   22.8699   44.2100
   29.0000   95.3448   19.0000   37.7300
   30.0000   82.7273   30.4394   39.1400

这篇关于MATLAB查找并将函数应用于重复索引的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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