matlab数组从零开始,MATLAB数组

MATLAB数组

在 MATLAB中,所有的所有数据类型的变量是多维数组。向量是一个一维阵列,矩阵是一个二维数组。

我们已经讨论过的向量和矩阵。在本章中,我们将讨论多维数组。然而,在这之前,让我们讨论一些特殊类型的数组。

MATLAB中的特殊阵列

在本节中,我们将讨论一些函数来创建一些特殊的阵列。对于所有这些函数,一个参数创建一个正方形阵列,双参数创建矩形阵列。

zeros() 函数创建一个数组的所有零:

例如:

zeros(5)

MATLAB 将执行上面的语句,并返回以下结果:

ans =

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

ones() 函数创建一个数组,所有的:

例如:

ones(4,3)

MATLAB将执行上面的语句,并返回以下结果:

ans =

1 1 1

1 1 1

1 1 1

1 1 1

eye() 函数创建一个矩阵。

例如:

eye(4)

MATLAB将执行上面的语句,并返回以下结果:

ans =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

rand() 函数创建一个数组(0,1)上均匀分布的随机数:

例如:

rand(3,5)

MATLAB将执行上面的语句,并返回以下结果:

ans =

0.8147 0.9134 0.2785 0.9649 0.9572

0.9058 0.6324 0.5469 0.1576 0.4854

0.1270 0.0975 0.9575 0.9706 0.8003

幻方

产生相同的总和,当它的元素加入逐行,逐列或对角线幻方是一个正方形。

magic() 函数创建一个幻方阵列。它需要一个单数的参数。该参数必须是一个标量大于或等于3。

magic(4)

MATLAB将执行上面的语句,并返回以下结果:

ans =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

多维数组

一个阵列,其具有两个以上的维度被称为多维数组在MATLAB中。在MATLAB中的多维数组是正常的两维矩阵的延伸。

一般生成一个多维数组,我们首先要创建一个二维数组,并扩展它。

例如,让我们创建一个二维数组a。

a=[795;619;432]

MATLAB将执行上面的语句,并返回以下结果:

a =

7 9 5

6 1 9

4 3 2

数组a是一个3x3阵列,我们可以添加一个第三维,通过提供的值,如:

a(:,:,2)=[123;456;789]

MATLAB将执行上面的语句,并返回以下结果:

a(:,:,1) =

7 9 5

6 1 9

4 3 2

a(:,:,2) =

1 2 3

4 5 6

7 8 9

我们也可以使用 ones(), zeros() 或 rand() 函数创建多维数组。

例如,

b=rand(4,3,2)

MATLAB将执行上面的语句,并返回以下结果:

b(:,:,1) =

0.0344 0.7952 0.6463

0.4387 0.1869 0.7094

0.3816 0.4898 0.7547

0.7655 0.4456 0.2760

b(:,:,2) =

0.6797 0.4984 0.2238

0.6551 0.9597 0.7513

0.1626 0.3404 0.2551

0.1190 0.5853 0.5060

我们也可以使用cat()函数来建立多维数组。它结合沿着指定的维度的数组列表:

cat() 函数的语法是:

B=cat(dim,A1,A2...)

这里:

B 是创建新的数组

A1, A2, ... 是要连接的阵列

dim 是一起串联阵列的维度

例子

创建一个脚本文件,并键入下面的代码:

a=[987;654;321];b=[123;456;789];c=cat(3,a,b,[231;478;390])

当运行该文件时,它会显示:

c(:,:,1) =

9 8 7

6 5 4

3 2 1

c(:,:,2) =

1 2 3

4 5 6

7 8 9

c(:,:,3) =

2 3 1

4 7 8

3 9 0

数组函数

MATLAB提供以下功能进行排序,旋转,置乱,重塑或移动数组的内容。

函数

目的

length

Length of vector or largest array dimension

ndims

Number of array dimensions

numel

Number of array elements

size

Array dimensions

iscolumn

Determine whether input is column vector

isempty

Determine whether array is empty

ismatrix

Determine whether input is matrix

isrow

Determine whether input is row vector

isscalar

Determine whether input is scalar

isvector

Determine whether input is vector

blkdiag

Construct block diagonal matrix from input arguments

circshift

Shift array circularly

ctranspose

Complex conjugate transpose

diag

Diagonal matrices and diagonals of matrix

flipdim

Flip array along specified dimension

fliplr

Flip matrix left to right

flipud

Flip matrix up to down

ipermute

Inverse permute dimensions of N-D array

permute

Rearrange dimensions of N-D array

repmat

Replicate and tile array

reshape

Reshape array

rot90

Rotate matrix 90 degrees

shiftdim

Shift dimensions

issorted

Determine whether set elements are in sorted order

sort

Sort array elements in ascending or descending order

sortrows

Sort rows in ascending order

squeeze

Remove singleton dimensions

transpose

Transpose

vectorize

Vectorize expression

示例

下面的例子说明一些上述函数。

长度,尺寸和数量的元素:

创建一个脚本文件,并键入下面的代码:

x=[7.1,3.4,7.2,28/4,3.6,17,9.4,8.9];length(x)%length of x vector

y=rand(3,4,5,2);ndims(y)%noof dimensionsinarray y

s=['Zara','Nuha','Shamim','Riz','Shadab'];numel(s)%noof elementsins

当您运行该文件,它会显示以下结果:

ans =

8

ans =

4

ans =

23

循环移位的数组元素:

创建一个脚本文件,并键入下面的代码:

a=[123;456;789]%the original array a

b=circshift(a,1)%circular shift first dimension values downby1.c=circshift(a,[1-1])%circular shift first dimension values%downby1%andsecond dimension values to the left%by1.

当您运行该文件,它会显示以下结果:

a =

1 2 3

4 5 6

7 8 9

b =

7 8 9

1 2 3

4 5 6

c =

8 9 7

2 3 1

5 6 4

数组排序

创建一个脚本文件,并键入下面的代码:

v=[2345129501917]%horizonal vector

sort(v)%sorting v

m=[264;539;201]%two dimensional array

sort(m,1)%sorting m along the row

sort(m,2)%sorting m along the column

当您运行该文件,它会显示以下结果:

v =

23 45 12 9 5 0 19 17

ans =

0 5 9 12 17 19 23 45

m =

2 6 4

5 3 9

2 0 1

ans =

2 0 1

2 3 4

5 6 9

ans =

2 4 6

3 5 9

0 1 2

单元阵列

单元阵列的阵列,其中每个单元格可以存储不同的维度和数据类型的数组的索引单元格。

单元格函数用于创建一个单元阵列。单元格函数的语法是:

C=cell(dim)C=cell(dim1,...,dimN)D=cell(obj)

这里

C 是单元阵列;

dim 是一个标量整数或整数向量,指定单元格阵列C的尺寸;

dim1, ... , dimN 是标量整数指定尺寸的C;

obj 是以下内容之一:

Java数组或对象

.NET阵列System.String类型或System.Object

例子

创建一个脚本文件,并键入下面的代码:

c=cell(2,5);c={'Red','Blue','Green','Yellow','White';12345}

当运行该文件,它会显示以下结果:

c =

'Red' 'Blue' 'Green' 'Yellow' 'White'

[ 1] [ 2] [ 3] [ 4] [ 5]

在单元格上阵列访问数据

有两种方法来引用的单元阵列的元素:

封闭的索引在第一个 bracket (),是指一组单元格

封闭的在大括号{},的索引单个单元格内的数据

括在第一支架的索引,它指的是单元格的集。

单元阵列索引平稳括号单元格集合。

例如:

c={'Red','Blue','Green','Yellow','White';12345};c(1:2,1:2)

MATLAB将执行上面的语句,并返回以下结果:

ans =

'Red' 'Blue'

[ 1] [ 2]

也可以用花括号索引访问单元格的内容。

例如:

c={'Red','Blue','Green','Yellow','White';12345};c{1,2:4}

MATLAB将执行上面的语句,并返回以下结果:

ans =

Blue

ans =

Green

ans =

Yellow

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值