`
deepfuture
  • 浏览: 4442728 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:80545
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:71180
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:104492
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:288308
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:15250
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:68842
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:32771
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:46478
社区版块
存档分类
最新评论

R-按维数生成数组与数组下标

 
阅读更多

向量变成数组,需要指定维数,下面的dim就指定了

> 1:15->z
> c(3,5)->dim(z)
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

arry可以更加方便
> array(1:15,dim=c(3,5),dimnames="mmm")
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

 

使用matrix也可以的

> matrix(1:15,nrow=3,ncol=5,byrow=TRUE)->Z
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15
>

 

byrow表示按行还是按列放置

 

 

下标

 

> z[3,1]
[1] 3
> z[3,]
[1]  3  6  9 12 15
> z[,5]
[1] 13 14 15
>

 > z[,2:5]
     [,1] [,2] [,3] [,4]
[1,]    4    7   10   13
[2,]    5    8   11   14
[3,]    6    9   12   15
>

取(1,2)、(3,4)、(1,3)的元素,将位置定义在数组中

> matrix(c(1,2,3,4,1,3),ncol=2,byrow=TRUE)->xx
> z[xx]
[1]  4 12  7
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15
>

 

 

 

 

 

 

matrix {base} R Documentation

Matrices

Description

matrix creates a matrix from the given set of values.

as.matrix attempts to turn its argument into a matrix.

is.matrix tests if its argument is a (strict) matrix.

Usage

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
       dimnames = NULL)

as.matrix(x, ...)
## S3 method for class 'data.frame'
as.matrix(x, rownames.force = NA, ...)

is.matrix(x)

Arguments

 

data

an optional data vector (including a list or expression vector). Other R objects are coerced by as.vector.

nrow

the desired number of rows.

ncol

the desired number of columns.

byrow

logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows.

dimnames

A dimnames attribute for the matrix: NULL or a list of length 2 giving the row and column names respectively. An empty list is treated as NULL, and a list of length one as row names. The list can be named, and the list names will be used as names for the dimensions.

x

an R object.

...

additional arguments to be passed to or from methods.

rownames.force

logical indicating if the resulting matrix should have character (rather than NULL) rownames. The default, NA, uses NULL rownames if the data frame has ‘automatic’ row.names or for a zero-row data frame.

Details

If one of nrow or ncol is not given, an attempt is made to infer it from the length of data and the other parameter. If neither is given, a one-column matrix is returned.

If there are too few elements in data to fill the matrix, then the elements in data are recycled. If data has length zero, NA of an appropriate type is used for atomic vectors (0 for raw vectors) and NULL for lists.

is.matrix returns TRUE if x is a vector and has a "dim" attribute of length 2) and FALSE otherwise. Note that a data.frame is not a matrix by this test. The function is generic: you can write methods to handle specific classes of objects, see InternalMethods.

as.matrix is a generic function. The method for data frames will return a character matrix if there is any non-(numeric/logical/complex) column, applying format to non-character columns. Otherwise, the usual coercion hierarchy (logical < integer < double < complex) will be used, e.g., all-logical data frames will be coerced to a logical matrix, mixed logical-integer will give a integer matrix, etc.

When coercing a vector, it produces a one-column matrix, and promotes the names (if any) of the vector to the rownames of the matrix.

is.matrix is a primitive function.

Note

If you just want to convert a vector to a matrix, something like

  dim(x) <- c(nx, ny)
  dimnames(x) <- list(row_names, col_names)

will avoid duplicating x.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

data.matrix, which attempts to convert to a numeric matrix.

A matrix is the special case of a two-dimensional array.

Examples

is.matrix(as.matrix(1:10))
!is.matrix(warpbreaks)# data.frame, NOT matrix!
warpbreaks[1:10,]
as.matrix(warpbreaks[1:10,]) #using as.matrix.data.frame(.) method

# Example of setting row and column names
mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE,
               dimnames = list(c("row1", "row2"),
                               c("C.1", "C.2", "C.3")))
mdat

Multi-way Arrays

Description

Creates or tests for arrays.

Usage

array(data = NA, dim = length(data), dimnames = NULL)
as.array(x, ...)
is.array(x)

Arguments

 

data

a vector (including a list or expression vector) giving data to fill the array. Other objects are coerced by as.vector.

dim

the dim attribute for the array to be created, that is a vector of length one or more giving the maximal indices in each dimension.

dimnames

either NULL or the names for the dimensions. This is a list with one component for each dimension, either NULL or a character vector of the length given by dim for that dimension. The list can be named, and the list names will be used as names for the dimensions. If the list is shorter than the number of dimensions, it is extended by NULLs to the length required

x

an R object.

...

additional arguments to be passed to or from methods.

Details

An array in R can have one, two or more dimensions. It is simply a vector which is stored with additional attributes giving the dimensions (attribute "dim") and optionally names for those dimensions (attribute "dimnames").

A two-dimensional array is the same thing as a matrix.

One-dimensional arrays often look like vectors, but may be handled differently by some functions: str does distinguish them in recent versions of R.

The "dim" attribute is an integer vector of length one or more containing non-negative values: the product of the values must match the length of the array.

The "dimnames" attribute is optional: if present it is a list with one component for each dimension, either NULL or a character vector of the length given by the element of the "dim" attribute for that dimension.

is.array is a primitive function.

Value

array returns an array with the extents specified in dim and naming information in dimnames. The values in data are taken to be those in the array with the leftmost subscript moving fastest. If there are too few elements in data to fill the array, then the elements in data are recycled. If data has length zero, NA of an appropriate type is used for atomic vectors (0 for raw vectors) and NULL for lists.

as.array is a generic function for coercing to arrays. The default method does so by attaching a dim attribute to it. It also attaches dimnames if x has names. The sole purpose of this is to make it possible to access the dim[names] attribute at a later time.

is.array returns TRUE or FALSE depending on whether its argument is an array (i.e., has a dim attribute of positive length) or not. It is generic: you can write methods to handle specific classes of objects, see InternalMethods.

Note

is.array is a primitive function.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

aperm, matrix, dim, dimnames.

Examples

dim(as.array(letters))
array(1:3, c(2,4)) # recycle 1:3 "2 2/3 times"
#     [,1] [,2] [,3] [,4]
#[1,]    1    3    2    1
#[2,]    2    1    3    2

分享到:
评论

相关推荐

    WORD版MATLAB经典入门教程连载2-第二章数值数组及其运算.doc

    与一维数组类似,可以使用子数组索引对二维数组进行部分访问和赋值,例如`b(1:end, 2:end-1) = [5 6]`。 2.6 执行数组运算的常用函数 2.6.1 函数数组运算规则的定义 MATLAB支持向量化运算,这意味着函数如`sin`、`...

    《物联网C#程序开发案例式教程》教学课件-第五章-数组与集合02 c#经典案例.pptx

    数组的下标通常从0开始,因此在遍历过程中,初始化索引变量`i`为0,然后在每次迭代中增加1,直到达到数组的长度。例如: ```csharp for (int i = 0; i 数组的长度; i++) { 数组名[i] = 值; } ``` 这段代码...

    第一章-MATLAB矩阵运算与数组运算.doc

    例如,`X=0:0.1:1` 生成一个从0到1,步长为0.1的一维数组。`linspace(0,pi,11)` 则创建了11个从0到π等间隔的数值。如果需要对数分隔的数组,`logspace` 函数可以帮助实现,例如 `x=logspace(1,2,10)` 会创建一个10...

    matlab 三维 数组 多维数组-创建多维数组-使用索引 算法开发、数据可视化、数据分析以及数值计算 Matlab课程

    结合`isosurface`等函数,可以生成三维图形,对于理解和解释三维数据非常有用。 在数据分析和数值计算中,多维数组可以用来处理高维数据集,如多变量统计分析或多元线性回归。例如,可以对每个变量进行建模,结果...

    杨辉三角-一维数组实现

    杨辉三角,又称帕斯卡三角,是一种在数学中广泛使用的二维数组模式,它由整数构成,每一行都是上一行的变形。每个数字是它正上方和左边两个数字的和。这个模式在组合数学、概率论、计算机科学等多个领域都有重要的...

    js对象根据下标变成数组

    js对象根据下标变成数组

    c++ 数组详解

    - 求最值与均值:遍历数组比较元素找出最大值和最小值,计算所有元素之和除以元素个数得到均值。 - 排序:常见的排序算法有冒泡排序、选择排序。例如,冒泡排序的基本思想是每次比较相邻元素,如果顺序错误就交换...

    DES加密算法 C实现 二维数组

    例如,可以定义一个二维数组来表示64位的数据块和56位的密钥,通过数组下标来表示位。在实现F函数时,可以创建多个一维数组来表示S-Box和P-Box,然后通过索引来完成替换和置换操作。 以下是一个简化版的C代码实现...

    三维数组-matlab

    在MATLAB中,可以使用三个下标来访问三维数组的元素: ```matlab value = A(i, j, k); % 访问三维数组A的第i行、第j列、第k深度的元素 ``` 同时,也可以使用线性索引来访问,但这种方式可能会降低代码的可读性。 ...

    二维数组分析.zip

    二维数组的元素访问通过两个下标完成,第一个下标表示行,第二个下标表示列。例如,访问arr数组的第一个元素(左上角)使用`arr[0][0]`。 在`StdAfx.cpp`和`二维数组分析.cpp`中,可能包含的是实现二维数组功能的...

    matlab创建三维数组的三种方法.docx

    三维数组可以看作是一种高维数组,它们的维数大于二维数组,具有更高的数据密度和复杂度。本文将介绍 Matlab 中创建三维数组的三种方法,分别是使用下标创建、使用低维数组创建和使用创建函数创建。 使用下标创建三...

    二维数组与字符数组.ppt

    二维数组在编程中是一种...以上代码分别实现了矩阵转置和杨辉三角形的生成,展示了二维数组在处理多维数据时的灵活性和实用性。理解并熟练掌握二维数组的定义、初始化和操作,是进行更复杂算法设计和问题求解的基础。

    matlab三维数组.test

    每个二维数组称为一层(或一张slice),而整个三维数组则可以通过三个下标进行访问:第一个下标代表行,第二个下标代表列,第三个下标则代表层。 #### 二、创建三维数组 在MATLAB中创建三维数组的方式有多种: 1. ...

    实验04 [构造类型程序设计1]--数组.doc

    实验04主要针对C语言中的构造类型程序设计,特别是数组的使用,旨在帮助初学者熟悉一维和二维数组的编程方法,以及相关的算法应用,如排序、查找和最值计算。以下是实验的详细内容和知识点解析: 1. **一维数组的...

    数组二维数组PPT学习教案.pptx

    二维数组是编程语言中一种非常基础且重要的数据结构,它被广泛应用于各种计算问题,特别是在处理表格型数据或矩阵运算时。在这个PPT学习教案中,我们深入探讨了二维数组的概念、定义、存储方式以及一些实际应用。 ...

    第五章 数组、字符串与类库

    数组下标从0开始,最大下标为数组元素个数减1。 - 避免下标越界和未初始化的数组错误,例如在访问数组元素前确保已分配内存,且下标在合法范围内。 2. 二维数组 - 定义:如 `int a[][];` 或 `int[][] b;` - 创建...

    C语言程序设计-把20个随机数存入一个数组,然后输出该数组中的最小值;其中确定最小值的下标的操作在fun函数中实现

    C语言程序设计-把20个随机数存入一个数组,然后输出该数组中的最小值;其中确定最小值的下标的操作在fun函数中实现,请给出该函数的定义;.c

    数据结构实验报告-数组与广义表-用顺序存储的半三角矩阵生成杨辉三角形5分-实验内容及要求.docx

    - **二维下标转换**:二维矩阵下标与以行序为主序的顺序数组下标之间的转换关系为 \( k = i*(i-1)/2 + j - 1 \)。 #### 算法设计 1. **初始化步骤**:首先输入要生成的杨辉三角形的行数 \( n \),然后计算该杨辉...

    (1)在Matlab中习惯性的会将二维数组中的第一维称为“行”第二维称为“列”,而对于三维数组的第三位则是习惯性的称为“页” 在

    1. **使用下标创建**:你可以通过循环来逐个填充数组元素,例如一个简单的三维数组可以通过三层嵌套循环实现。这允许你根据需要自定义每个元素的值。如: ```matlab for i=1:2 for j=1:2 for k=1:2 A(i,j,k)=i+...

    LeetCode刷题记录(简单-数组)

    **题解**: 给定一个已排序的整数数组和一个目标值,找到数组中和为目标值的两个数,并返回它们的下标。 **算法**: - 使用双指针方法,一个指针指向数组开头,另一个指向结尾。 - 如果两个指针指向的数之和小于目标...

Global site tag (gtag.js) - Google Analytics