提取唯一值

Osc*_*car 3 matlab uniqueidentifier

我有两列中的数据,如下所示:

A               B

1,265848208     3
-0,608043611    0
-0,285735893    0
0,006895134     7
0               7
-0,004526196    7
0,176326617     10
-0,159688071    2
0,22439945      2
-0,991045044    1
0,178022324     1
-0,270967397    4
0,285849994     4
1,881705539     23
1,057184204     10
NaN             10

对于BI中的所有唯一值,要在A列中提取相应的值并将其移动到新的矩阵.我希望然后计算A中所有相应值的平均值,并在回归中用作因变量(在B中每个值的观察值加权),其中B的公共值是减少噪声的自变量.任何关于如何在Matlab中执行此操作的帮助(运行回归除外)都会很棒!

谢谢

奥斯卡

Amr*_*mro 5

这是一个有效的解决方案:

X = [
    1.265848208     3
    -0.608043611    0
    -0.285735893    0
    0.006895134     7
    0               7
    -0.004526196    7
    0.176326617     10
    -0.159688071    2
    0.22439945      2
    -0.991045044    1
    0.178022324     1
    -0.270967397    4
    0.285849994     4
    1.881705539     23
    1.057184204     10
    NaN             10
];

%# unique values in B, and their indices
[valB,~,subs] = unique(X(:,2));

%# values of A for each unique number in B (cellarray)
valA = accumarray(subs, X(:,1), [], @(x) {x});

%# mean of each group
meanValA = cellfun(@nanmean, valA)

%# perform regression here...
Run Code Online (Sandbox Code Playgroud)

结果:

%# B values, mean of corresponding values in A, number of A values
>> [valB meanValA cellfun(@numel,valA)]
ans =
            0     -0.44689            2
            1     -0.40651            2
            2     0.032356            2
            3       1.2658            1
            4    0.0074413            2
            7   0.00078965            3
           10      0.61676            3
           23       1.8817            1
Run Code Online (Sandbox Code Playgroud)