reshape - Reshape array

from:http://www.mathworks.com/access/helpdesk/help/techdoc/ref/reshape.html

 

reshape - Reshape array

Syntax

B = reshape(A,m,n)
B = reshape(A,m,n,p,...)
B = reshape(A,[m n p ...])
B = reshape(A,...,[],...)
B = reshape(A,siz)

Description

B = reshape(A,m,n) returns the m -by-n matrix B whose elements are taken column-wise from A . An error results if A does not have m*n elements.

B = reshape(A,m,n,p,...) or B = reshape(A,[m n p ...]) returns an n-dimensional array with the same elements as A but reshaped to have the size m -by-n -by-p -by-... . The product of the specified dimensions, m*n*p* ..., must be the same as prod(size(A)) .

B = reshape(A,...,[],...) calculates the length of the dimension represented by the placeholder [] , such that the product of the dimensions equals prod(size(A)) . The value of prod(size(A)) must be evenly divisible by the product of the specified dimensions. You can use only one occurrence of [] .

B = reshape(A,siz) returns an n-dimensional array with the same elements as A , but reshaped to siz , a vector representing the dimensions of the reshaped array. The quantity prod(siz) must be the same as prod(size(A)) .

Examples

Reshape a 3 -by-4 matrix into a 2 -by-6 matrix.

A =
1 4 7 10
2 5 8 11
3 6 9 12

B = reshape(A,2,6)

B =
1 3 5 7 9 11
2 4 6 8 10 12
B = reshape(A,2,[])

B =
1 3 5 7 9 11
2 4 6 8 10 12
### 回答1: array.reshape(-1, 1) 的作用是将一个数组重新排列成一个列数为1的二维数组。其中参数 -1 表示行数由程序自动推导出来,以保证数组元素总数不变。这个操作常用于将一维数组转换为列向量,或者在某些机器学习算法中将输入数据的维度进行调整。 ### 回答2: array.reshape(-1, 1)是一种数组重塑的操作。这个操作可以使得原始数组变为一个只有一列的二维数组。 参数-1表示自动计算数组的行数。在这种情况下,reshape函数会根据原始数组的大小和列数计算出新的行数。而参数1表示新数组的列数,也就是1列。 这种操作主要用于需要将原始数组变为列向量的情况。例如,原始数组是一个n维数组,通过reshape(-1, 1)之后,就可以将该数组变为只有1列的二维数组,每一行对应原数组的一个元素。 通过这种重塑操作,可以方便地进行一些需要特定形状的计算。例如,在机器学习中,常常需要将特征向量整理成一列,以便用于训练模型。 总之,array.reshape(-1, 1)是一种用于重塑数组形状的操作,可以将原始数组变为只有1列的二维数组,方便进行一些特定形状要求的计算。 ### 回答3: array.reshape(-1, 1)是一个NumPy数组的方法,用于重新设置数组的形状。其中的-1表示自动计算数组的行数或列数,而1表示数组的列数为1。 通过reshape(-1, 1),我们可以将一个一维数组转换为一个二维列向量。具体来说,如果原始数组有n个元素,那么reshape(-1, 1)将会得到一个n行1列的数组。 举个例子,假设我们有一个一维数组array,其内容为[1, 2, 3, 4, 5],如果我们调用array.reshape(-1, 1),将会得到一个二维数组: [[1] [2] [3] [4] [5]] 可以看到,原始数组被重新排列为5行1列的形式。 这种重新设置数组形状的操作常用于数据处理和机器学习中。例如,在一些机器学习算法中,我们需要将输入数据转换为特定的形状,以便进行训练或预测。reshape(-1, 1)提供了一种方便的方式,可以快速将一维数组转换为列向量,使得数据满足算法的输入要求。 总之,array.reshape(-1, 1)是一种常用的NumPy数组操作,用于将一维数组重新排列为n行1列的二维数组形式。这个方法在数据处理和机器学习中有着广泛的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值