将矩阵转换为一维数组 [英] Convert a matrix to a 1 dimensional array

查看:31
本文介绍了将矩阵转换为一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵(32X48).

I have a matrix (32X48).

如何将矩阵转换为一维数组?

How can I convert the matrix into a single dimensional array?

推荐答案

要么用 'scan' 读入,要么在矩阵上执行 as.vector().如果需要按行或列转置矩阵,则可能需要先转置矩阵.

Either read it in with 'scan', or just do as.vector() on the matrix. You might want to transpose the matrix first if you want it by rows or columns.

> m=matrix(1:12,3,4)
> m
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> as.vector(m)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
> as.vector(t(m))
 [1]  1  4  7 10  2  5  8 11  3  6  9 12

这篇关于将矩阵转换为一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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