简体   繁体   English

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

[英]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?

So for example I have 所以我举个例子

1st column | 第1列| 2nd column 第二栏

1             1
1             3
1             9
2             4
2             7

I want to convert it to 我想把它转换成

1st column | 第1列| 2nd column | 第二栏| 3rd column | 第3栏| 4th column 第四栏

1             1           3           9
2             4           7           3

The (3,4) element should be empty. (3,4)元素应为空。

I can do it by Matlab using for and if but it takes too much time for huge data, so I need a more elegant and brilliant idea. 我可以通过Matlab使用for和if来实现它,但是它需要太多时间来处理大量数据,所以我需要一个更优雅和更棒的想法。

I prefer Matlab but other languages are ok. 我更喜欢Matlab,但其他语言还可以。 (I can export the matrix to csv or xlsx or txt and use the other languages, if that language can solve my problem.) (我可以将矩阵导出到csv或xlsx或txt并使用其他语言,如果该语言可以解决我的问题。)

Thank you in advance! 先感谢您!

[Updates] [更新]

If 如果

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

1st column | 第1列| 2nd column | 第二栏| 3rd column 第3栏

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 | 第3列| 4th col | 第4列| 5th col | 第5列| 6th col| 第6列| 7th col 第7集

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

How can I do this? 我怎样才能做到这一点? Thank you in advance! 先感谢您!

Use accumarray with a custom function 使用自定义函数的accumarray

>> r = accumarray( A(:,1), A(:,2), [], @(x) {x'} ); %//'
 r = 
  [1x3 double]
  [1x2 double]
>> r{1}
 ans =
  1     3     9
>> r{2}
 ans =
  4     7

Update: 更新:
Converting cell r to a matrix B (accomodating further requests in comments): 将单元格r转换为矩阵B (在评论中容纳更多请求):

>> [U ix iu] = unique( A(:,1) ); % see EitantT's comment
>> r = accumarray( iu, A(:,2), [], @(x) {x'} ); 
>> n = cellfun( @numel, r ); % fund num elements in each row - need for max
>> mx = max(n);
>> pad = 555555; % padding value
>> r = cellfun( @(x) [x pad*ones(1,mx - numel(x))], r, 'uni', 0 );
>> B = vertcat( r{:} ); % construct B from padded rows of r

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 合并多个文件:第一列(相同的字符串),第二列(每个文件的唯一值) - Merge multiple files: 1st Column (same string), 2nd Column (unique values per file) seaborn gridplot/subplots 在第一列显示一个变量,在第二列显示另一个变量,用于同一行 ID - seaborn gridplot/subplots to show one variable on the 1st column and another variable on 2nd column for the same row ID Pandas 增加一列表示第1和第2位,根据行值 - Pandas to add a column to indicate the 1st and 2nd places, according to row values 如何为具有相同第一行值的每个第二行值快速处理 2xN 列表/nparray? - How to fast process 2xN list/nparray for each 2nd row value which has the same 1st row value? 将 pandas 数据框列中的每个值与第二个数据框列的所有值相乘并将每个第一个数据框值替换为结果数组 - Multiply each value in a pandas dataframe column with all values of 2nd dataframe column & replace each 1st dataframe value with resulting array 两个 CSV 文件,匹配一行中的一对与第二个 CSV 文件中的匹配值,在由相同类型的值组成的单个列中 - Two CSV files, match a pair from a row with matching values in 2nd CSV file, in a single column consisting of the same type of values 当我将第一列作为输入传递给函数时,从 csv 文件中获取第二列记录 - Get the 2nd column records from the csv file when i pass 1st column as a input to the function 使用beautifulsoup4(第2行,第1列和第6列)从html表中提取值 - extracting values from html table using beautifulsoup4 (2nd row onwards, 1st and 6th column) awk比较3个值,第一个文件值之间的第二个文件值和两个文件之间的多列打印输出到第3个 - Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd 根据第二列中存在的字符串更新第一列 - Update 1st column based on string present in 2nd column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM