简体   繁体   English

在MatLab中有效的R中的reshape2中是否存在类似的“融化”功能?

[英]Is there a similar function to 'melt' in reshape2 in R that works in MatLab?

I'm trying to find a function in MatLab that is similar to the 'melt' function in the R package "reshape2", such that the row headers are repeated for each variable and stacked together. 我试图在MatLab中找到一个类似于R包“reshape2”中的'melt'函数的函数,这样就可以为每个变量重复行标题并将它们堆叠在一起。

eg: 例如:

If I have a matrix 如果我有一个矩阵

A  1  2  3 
B  4  5  6
C  7  8  9

I'd like to change it to 我想改成它

A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
C 9

Short of working a for() loop to go pair-wise through each column, is there a function that could do this? 如果没有使用for()循环来成对地通过每一列,是否有一个函数可以做到这一点?

Many thanks, KRB 非常感谢,KRB

Matlab has stack and unstack functions, operating on tables, which are similar to melt and cast . MATLAB具有stackunstack功能,上表中,其类似于操作meltcast Something like this would work 像这样的东西会起作用

groups = {'A'; 'B'; 'C'};
A1 = [1; 4; 7];
A2 = [2; 5; 8];
A3 = [3 ; 6; 9];

T = table(groups, A1, A2, A3)
TLong = stack(T, 2:4)

which gives 这使

TLong = 

    groups    A1_A2_A3_Indicator    A1_A2_A3
    ______    __________________    ________

    'A'       A1                    1       
    'A'       A2                    2       
    'A'       A3                    3       
    'B'       A1                    4       
    'B'       A2                    5       
    'B'       A3                    6       
    'C'       A1                    7       
    'C'       A2                    8       
    'C'       A3                    9       

Note that your example matrix cannot exist in Matlab (or R) as matrices cannot contain strings or mixed types. 请注意,您的示例矩阵不能存在于Matlab(或R)中,因为矩阵不能包含字符串或混合类型。 I recommend you convert whatever structure you have now to a table if you want to use a inbuilt function. 如果您想使用内置函数,我建议您将现有的任何结构转换为table

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM