博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
as.vector 矩阵向量化
阅读量:5743 次
发布时间:2019-06-18

本文共 5080 字,大约阅读时间需要 16 分钟。

将矩阵向量化有时候是必要的。矩阵的向量化可以通过as.vector()来实现:
> A     [,1] [,2] [,3] [,4][1,]    1    4    7   10[2,]    2    5    8   11[3,]    3    6    9   12
将矩阵元素向量化, 注意是逐列转换的:
> as.vector(A) [1]  1  2  3  4  5  6  7  8  9 10 11 12
如果要逐行转换, 可以使用t转换一下 : 
> as.vector(t(A)) [1]  1  4  7 10  2  5  8 11  3  6  9 12
将矩阵的方阵部分元素向量化:
> dim(A)[1] 3 4> ncol(A)[1] 4> nrow(A)[1] 3> A[1:min(dim(A)),1:min(dim(A))]     [,1] [,2] [,3][1,]    1    4    7[2,]    2    5    8[3,]    3    6    9> as.vector(A[1:min(dim(A)),1:min(dim(A))])[1] 1 2 3 4 5 6 7 8 9
[参考]
1. > help(as.vector)
vector                  package:base                   R DocumentationVectorsDescription:     ‘vector’ produces a vector of the given length and mode.     ‘as.vector’, a generic, attempts to coerce its argument into a     vector of mode ‘mode’ (the default is to coerce to whichever     vector mode is most convenient): if the result is atomic all     attributes are removed.     ‘is.vector’ returns ‘TRUE’ if ‘x’ is a vector of the specified     mode having no attributes _other than names_.  It returns ‘FALSE’     otherwise.Usage:     vector(mode = "logical", length = 0)     as.vector(x, mode = "any")     is.vector(x, mode = "any")     Arguments:    mode: character string naming an atomic mode or ‘"list"’ or          ‘"expression"’ or (except for ‘vector’) ‘"any"’.  length: a non-negative integer specifying the desired length.  For a          long vector, i.e., ‘length > .Machine$integer.max’, it has to          be of type ‘"double"’.  Supplying an argument of length other          than one is an error.       x: an R object.Details:     The atomic modes are ‘"logical"’, ‘"integer"’, ‘"numeric"’     (synonym ‘"double"’), ‘"complex"’, ‘"character"’ and ‘"raw"’.     If ‘mode = "any"’, ‘is.vector’ may return ‘TRUE’ for the atomic     modes, ‘list’ and ‘expression’.  For any ‘mode’, it will return     ‘FALSE’ if ‘x’ has any attributes except names.  (This is     incompatible with S.)  On the other hand, ‘as.vector’ removes     _all_ attributes including names for results of atomic mode (but     not those of mode ‘"list"’ nor ‘"expression"’).     Note that factors are _not_ vectors; ‘is.vector’ returns ‘FALSE’     and ‘as.vector’ converts a factor to a character vector for ‘mode     = "any"’.Value:     For ‘vector’, a vector of the given length and mode.  Logical     vector elements are initialized to ‘FALSE’, numeric vector     elements to ‘0’, character vector elements to ‘""’, raw vector     elements to ‘nul’ bytes and list/expression elements to ‘NULL’.     For ‘as.vector’, a vector (atomic or of type list or expression).     All attributes are removed from the result if it is of an atomic     mode, but not in general for a list result.  The default method     handles 24 input types and 12 values of ‘type’: the details of     most coercions are undocumented and subject to change.     For ‘is.vector’, ‘TRUE’ or ‘FALSE’.  ‘is.vector(x, mode =     "numeric")’ can be true for vectors of types ‘"integer"’ or     ‘"double"’ whereas ‘is.vector(x, mode = "double")’ can only be     true for those of type ‘"double"’.Methods for 'as.vector()':     Writers of methods for ‘as.vector’ need to take care to follow the     conventions of the default method.  In particular        ? Argument ‘mode’ can be ‘"any"’, any of the atomic modes,          ‘"list"’, ‘"expression"’, ‘"symbol"’, ‘"pairlist"’ or one of          the aliases ‘"double"’ and ‘"name"’.        ? The return value should be of the appropriate mode.  For          ‘mode = "any"’ this means an atomic vector or list.        ? Attributes should be treated appropriately: in particular          when the result is an atomic vector there should be no          attributes, not even names.        ? ‘is.vector(as.vector(x, m), m)’ should be true for any mode          ‘m’, including the default ‘"any"’.Note:     ‘as.vector’ and ‘is.vector’ are quite distinct from the meaning of     the formal class ‘"vector"’ in the ‘methods’ package, and hence     ‘as(x, "vector")’ and ‘is(x, "vector")’.     Note that ‘as.vector(x)’ is not necessarily a null operation if     ‘is.vector(x)’ is true: any names will be removed from an atomic     vector.     Non-vector ‘mode’s ‘"symbol"’ (synonym ‘"name"’) and ‘"pairlist"’     are accepted but have long been undocumented: they are used to     implement ‘as.name’ and ‘as.pairlist’, and those functions should     preferably be used directly.  None of the description here applies     to those ‘mode’s: see the help for the preferred forms.References:     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S     Language_.  Wadsworth & Brooks/Cole.See Also:     ‘c’, ‘is.numeric’, ‘is.list’, etc.Examples:     df <- data.frame(x = 1:3, y = 5:7)     ## Error:     try(as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric"))          x <- c(a = 1, b = 2)     is.vector(x)     as.vector(x)     all.equal(x, as.vector(x)) ## FALSE               ###-- All the following are TRUE:     is.list(df)     ! is.vector(df)     ! is.vector(df, mode = "list")          is.vector(list(), mode = "list")

转载地址:http://xtszx.baihongyu.com/

你可能感兴趣的文章
《11招玩转网络安全》之第一招:Docker For Docker
查看>>
7、kvm虚拟机快照备份
查看>>
visual studio 2005没有找到MSVCR80D.dll问题
查看>>
hive_0.11中文用户手册
查看>>
hiveserver2修改线程数
查看>>
我的友情链接
查看>>
XML教程
查看>>
AS3.0 Array常用方法总结
查看>>
oracle体系结构
查看>>
J2音乐房
查看>>
Microsoft Exchange Server 2010与Office 365混合部署升级到Exchange Server 2016混合部署汇总...
查看>>
Proxy服务器配置_Squid
查看>>
nagios在apache上安装配置
查看>>
开启“无线网络”,提示:请启动windows零配置wzc服务
查看>>
【SDN】Openflow协议中对LLDP算法的理解--如何判断非OF区域的存在
查看>>
纯DIV+CSS简单实现Tab选项卡左右切换效果
查看>>
栈(一)
查看>>
LINUX档案权限
查看>>
数据结构及算法基础--优先队列(Priority Queue)
查看>>
C#使用Timer.Interval指定时间间隔与指定时间执行事件
查看>>