如何处理成对的值? [英] How to deal with paired values?

查看:163
本文介绍了如何处理成对的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个独立变量值的向量

Say I have a vector of independent variable values

v =[ 1 2 2 1 1 .5 1 2 .5 .5 1]

和响应变量的向量

u = [ 5 22 20 4 8 .2 5 12 0 .5 6]

我想用误差线绘制uv的关系,该方法需要为自变量使用100个可能的值.问题不在于绘制误差线,而在于如何创建向量对[mean(u(find(v==0.5)), mean(u(find(v==1)), mean(u(find(v==2))].除了对v进行排序,然后挑选排序后的v的值并找到v的索引(其中v与这些值匹配)之外,是否有标准的自动化方法?这似乎效率很低.

I want to plot u vs. v with errorbars, the method needs to work for 100s of possible values for the independent variable. The problem isn't in plotting the error bars, it's in how can I create a vector pair [mean(u(find(v==0.5)), mean(u(find(v==1)), mean(u(find(v==2))]. Is there a standard automated way of doing this other than sorting v, then picking out the values of sorted v and finding the index of v where v matches those values? This seems very inefficient.

推荐答案

如果要按v的唯一值在<v:

>> [unv,iunv,iv] = unique(v);
>> umeans = accumarray(iv(:),u,[],@mean);
>> [~,ivorder] = sort(iunv);
>> umeans = umeans(ivorder)

umeans =

    5.6000
   18.0000
    0.2333

如果要按v的排序值的平均值求平均值,则只需使用accumarray的输出,而无需重新排序命令:

If you want the means in order of the sorted values of v, then just use the output of accumarray without the reordering commands:

>> [unv,iunv,iv] = unique(v);
>> umeans = accumarray(iv(:),u,[],@mean)

umeans =

    0.2333
    5.6000
   18.0000

只需确保u是行向量.

这篇关于如何处理成对的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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