R 或 MATLAB - 匹配列名称以修改矩阵中的相应单元格

发布于 2024-10-30 05:58:58 字数 845 浏览 0 评论 0原文

我想将下面矩阵中以 NODE 作为标题的列转换为其相应的基因,如下所示:

   NODE Gene1 Gene2 Gene3 Gene4 Gene5 NODE1 NODE2 NODE3
1 NODE1  0.98  0.98    NA    NA    NA    NA    NA    NA
2 NODE2    NA    NA   0.8   0.8    NA    NA    NA    NA
3 NODE3    NA    NA    NA    NA  0.72  0.72    NA    NA
4 NODE4    NA    NA    NA    NA    NA    NA   0.6   0.6

因为

NODE1 = Gene1 and Gene2
NODE2 = Gene3 and Gene4
NODE3 = Gene5 and NODE1 = Gene5 and Gene 1 and Gene2

我想将 NODE1 NODE2 和 NODE3 列中的单元格移动到其相应的基因列以输出如下文件:

   NODE Gene1 Gene2 Gene3 Gene4 Gene5 
1 NODE1  0.98  0.98    NA    NA    NA    
2 NODE2    NA    NA   0.8   0.8    NA    
3 NODE3  0.72  0.72    NA    NA  0.72    
4 NODE4  0.6   0.6     NA   0.6  0.6  

是否有这样做的方法?我可以使用 R 或 MATLAB 来执行此操作吗?

最好的问候,

凯瑟琳

I would like to convert the column with NODE as header in a Matrix below to their corresponding Gene as follows:

   NODE Gene1 Gene2 Gene3 Gene4 Gene5 NODE1 NODE2 NODE3
1 NODE1  0.98  0.98    NA    NA    NA    NA    NA    NA
2 NODE2    NA    NA   0.8   0.8    NA    NA    NA    NA
3 NODE3    NA    NA    NA    NA  0.72  0.72    NA    NA
4 NODE4    NA    NA    NA    NA    NA    NA   0.6   0.6

As

NODE1 = Gene1 and Gene2
NODE2 = Gene3 and Gene4
NODE3 = Gene5 and NODE1 = Gene5 and Gene 1 and Gene2

I would like to move cells in NODE1 NODE2 and NODE3 columns to their corresponding Gene colunms to output a file like this:

   NODE Gene1 Gene2 Gene3 Gene4 Gene5 
1 NODE1  0.98  0.98    NA    NA    NA    
2 NODE2    NA    NA   0.8   0.8    NA    
3 NODE3  0.72  0.72    NA    NA  0.72    
4 NODE4  0.6   0.6     NA   0.6  0.6  

Are there any methods of doing this? Can I use R or MATLAB to do this?

Best regards,

Catherine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-11-06 05:58:58

在 Matlab 中,您可以这样做:

    clear all
    nrgenes=5;
    nrnodes=4;
    matrix=zeros(nrnodes,nrgenes+nrnodes);
    matrix(1,1:2)=0.98;
    matrix(2,3:4)=0.8;
    matrix(3,5:6)=0.72;
    matrix(4,7:8)=0.6;

    for i=1:nrnodes
        for j=nrgenes+1:nrgenes+nrnodes
            if (matrix(i,j)~=0)
                matrix(i,1:nrgenes)=matrix(i,1:nrgenes)+matrix(i,j)*logical(matrix(j-nrgenes,1:nrgenes));
            end
        end
    end
    result=matrix(1:nrnodes,1:nrgenes);

一些注意事项:

  • 您可能需要运行两次才能根据条目的配置来展开所有条目,但我认为您已经从代码中了解了如何执行此操作。< /p>

  • 示例结果的 Node4 行存在轻微错误。

  • 这是用两个嵌套的 for 循环编码的。对于大型矩阵来说缓慢且低效,但更容易理解。您可能必须对大型矩阵进行矢量化以缩短执行时间。

In Matlab you can do it like this:

    clear all
    nrgenes=5;
    nrnodes=4;
    matrix=zeros(nrnodes,nrgenes+nrnodes);
    matrix(1,1:2)=0.98;
    matrix(2,3:4)=0.8;
    matrix(3,5:6)=0.72;
    matrix(4,7:8)=0.6;

    for i=1:nrnodes
        for j=nrgenes+1:nrgenes+nrnodes
            if (matrix(i,j)~=0)
                matrix(i,1:nrgenes)=matrix(i,1:nrgenes)+matrix(i,j)*logical(matrix(j-nrgenes,1:nrgenes));
            end
        end
    end
    result=matrix(1:nrnodes,1:nrgenes);

Some caveats:

  • You maybe have to run this twice to expand all the entries depending on the configuration of entries, but I think you get the idea how to do this from the code.

  • there's a slight error in the Node4 line of your example result.

  • This is coded with two nested for loops. Slow and inefficient for large matrices, but easier to understand. You maybe have to vectorize for large matrices to improve execution time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文