两个矩阵MATLAB中的不同数组的大小比较 [英] Comparing two matrices in MATLAB with different array sizes

查看:1708
本文介绍了两个矩阵MATLAB中的不同数组的大小比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里是我的样本数据:

 编曲STARP:
2141865 16
2141865 17
2141865 17
2141865 16
2141865 17
2141865 20
2141865 9
2141865 7
2141865 19
2141865 18
2141865 19
2141865 9
2141865 9
2141865 9
2141865 9
编曲中位数:
2141865 16

的目标是比较编曲STARP给Arr位数和发现如果与编曲STARP一个相应的ID(2141865)的任何值是在一定的范围为在编曲位数该ID的值的范围内(如果它是1.5 *大于或1.5 *更小),并且如果它不那么它需要被乘或1.5分。如果编曲STARP的值是2 *大于编曲位数更大或更小,然后我想在新的平均计算忽略该值。

输出示例:

  2141865 14.00666667

P.S。编曲B的实际尺寸是198x2和编曲A是45879x2,编曲A有很多很多不同的ID在它和编曲B有确切198,编曲B的距离与匹配的ID找出所有在编曲A中值的中位数派生和输出的中位数与ID一起。


解决方案

 函数[中位数,newAverage] = Medians_Koi(StarData,R,M)    %// R是此范围以外,其中值必须被相乘。我假设它的中位数方面pssed前$ P $,所以例如,如果R为[0.8,1.2]和中位数为10,则允许的范围为8  -  12。
    //%M是由什么乘以(这真的应该在这个问题已经解释)    //%你为什么要与全球和evalin这样做?这是真的真的不好的做法,没有理由。只要函数接受输入参数和输出输出参数
    //%= StarData evalin('基地','STARP');
    //%全球中位数;
    //%全球newAvg;    StarData2 = StarData;    [编号,〜,Groups_1] =独特(StarData(:,1),稳定);
    中位数= [标识,accumarray(Groups_1,StarData(:,2),[],@median)];    对于G = 1:尺寸(Group_1,1)
        toMultiply =(StarData(:,1)==标识(G))及(StarData(:,2)&所述;中位数(G)* R(1));
        toDivide =(StarData(:,1)==标识(G))及(StarData(:,2)>中位数(G)* R(2));
        StarData2(toMultiply,2)= StarData(toMultiply,2)* M(1)。
        StarData2(toDivide,2)= StarData(toDivide,2)./ M(2);
    结束    newAverage = [标识,accumarray(Groups_1,StarData2(:,2),[],@mean)];结束

So here is my sample Data:

Arr StarP:
2141865 16
2141865 17
2141865 17
2141865 16
2141865 17
2141865 20
2141865 9
2141865 7
2141865 19
2141865 18
2141865 19
2141865 9
2141865 9
2141865 9
2141865 9
Arr medians:
2141865 16

The goal is to compare Arr StarP to Arr medians and find if any value with a corresponding ID (2141865) in Arr StarP is within a certain range of the value for that ID in Arr medians (if it is 1.5* larger or 1.5* smaller), and if it is not then it needs to be multiplied or divided by 1.5. If the value of Arr StarP is 2* larger or smaller than Arr medians then I want that value ignored in the new average calculation.

Example Output:

2141865 14.00666667

P.S. The real size of Arr B is 198x2 and Arr A is 45879x2, Arr A has many many different ID's in it and Arr B has exactly 198, Arr B was derived from finding the medians of all of the values in Arr A with a matching ID and outputting that median along with the ID.

解决方案

function [medians, newAverage] = Medians_Koi(StarData, R, M)

    %//R is this range outside of which values must be multiplied. I'm assuming it's expressed in terms of the median, so for example if R is [0.8, 1.2] and the median is 10 then the range allowed is 8 - 12.
    %//M is what to multiply by (this really should have been explained in the question)

    %//why are you doing this with global and evalin?? This is really really bad practice and there is no reason for it. Just make the function accept input arguments and output output arguments
    %//StarData = evalin('base', 'StarP');
    %//global medians;
    %//global newAvg;

    StarData2 = StarData;

    [IDs, ~, Groups_1] = unique(StarData(:,1),'stable');
    medians = [IDs, accumarray(Groups_1, StarData(:,2), [], @median)];

    for g = 1:size(Group_1,1)
        toMultiply = (StarData(:,1)==IDs(g)) & (StarData(:,2) < medians(g)*R(1));
        toDivide = (StarData(:,1)==IDs(g)) & (StarData(:,2) > medians(g)*R(2));
        StarData2(toMultiply,2) = StarData(toMultiply,2) .* M(1);
        StarData2(toDivide,2) = StarData(toDivide,2) ./ M(2);
    end

    newAverage = [IDs, accumarray(Groups_1, StarData2(:,2), [], @mean)];

end

这篇关于两个矩阵MATLAB中的不同数组的大小比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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