按不同行的第一列索引对值进行分组 [英] Group values in different rows by their first-column index

查看:159
本文介绍了按不同行的第一列索引对值进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是 MatLab(或任何其他语言)转换矩阵或csv将第二列值放到同一行,如果第一列的值相同?



如果

  A = [2 3 234; 2 44 33; 2 12 22; 3 123 99; 3 1232 45; 5 224 57] 



第二列|第3栏



  2 3 234 
2 44 33
2 12 22
3 123 99
3 1232 45
5 224 57

然后运行

  [U ix iu] = unique(A(:,1)); 
r = accumarray(iu,A(:,2:3),[],@(x){x'})

会显示错误

 使用accumarray时出错
第二个输入VAL必须SUBS中每行一个元素的向量,或
标量。

我想让



| 2nd col | 3rd col |第4栏|第5列| 6th col |第7集



  2 3 234 44 33 12 22 
3 123 99 1232 45
5 224 57

我知道如何使用for和if,但是花费大量时间用于大数据。 / p>

我该如何做?



提前感谢!

解决方案

code> accumarray 中提供的解决方案。第一个参数 iu 是索引的向量,第二个参数应该是相同长度的值的向量。你在这里做的是指定一个矩阵作为第二个参数,其实是比 iu 中的索引多两倍的值。



为了使它工作,你需要做的是为第二列和第三列创建一个索引向量(它们是相同的索引,而不是巧合!),并指定一个匹配的值列向量,像这样:

  [U,ix,iu] = unique(A(:,1)); 
vals = reshape(A(:, 2:end)。',[],1); %'//列化值
subs = reshape(iu(:, ones(size(A,2) - 1,1))',[],1) %'//复制索引
r = accumarray(subs,vals,[],@(x){x'});

此解决方案适用于您要传递给 accumarray


This question is an outgrowth of MatLab (or any other language) to convert a matrix or a csv to put 2nd column values to the same row if 1st column value is the same?

If

      A = [2 3 234 ; 2 44 33; 2 12 22; 3 123 99; 3 1232 45; 5 224 57]

1st column | 2nd column | 3rd column

2             3          234
2             44         33
2             12         22
3             123        99
3             1232       45
5             224        57

then running

    [U ix iu] = unique(A(:,1) ); 
    r= accumarray( iu, A(:,2:3), [], @(x) {x'} )

will show me the error

    Error using accumarray
    Second input VAL must be a vector with one element for each row in SUBS, or a
    scalar.

I want to make

1st col | 2nd col | 3rd col | 4th col | 5th col | 6th col| 7th col

2         3        234       44        33        12        22
3         123      99        1232      45
5         224      57

I know how to do it using for and if, but that spends too much time for big data.

How can I do this?

Thank you in advance!

解决方案

You're misusing accumarray in the solution provided to your previous question. The first parameter iu is the vector of indices and the second parameter should be a vector of values, of the same length. What you did here is specify a matrix as the second parameter, which in fact has twice more values than indices in iu.

What you need to do in order to make it work is create a vector of indices both for the second column and for the third column (they are the same indices, not coincidentally!) and specify a matching column vector of values, like so:

[U, ix, iu] = unique(A(:,1));
vals = reshape(A(:, 2:end).', [], 1);                    %'// Columnize values
subs = reshape(iu(:, ones(size(A, 2) - 1, 1)).', [], 1); %'// Replicate indices
r = accumarray(subs, vals, [], @(x){x'});

This solution is generalized for any number of columns that you want to pass to accumarray.

这篇关于按不同行的第一列索引对值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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