matlab冒号和减号,MATLAB冒号符号

MATLAB冒号符号

冒号(:)是最有用的运算符在MATLAB之一。它是用来创建矢量,下标数组和指定的迭代。

如果想创建一个行向量,包含从1到10的整数,如下:

1:10

MATLAB执行该语句,并返回一个行向量,包含从1到10的整数:

ans =

1 2 3 4 5 6 7 8 9 10

如果想指定以外的一个增量值,例如:

100:-5:50

MATLAB执行该语句,并返回以下结果:

ans =

100 95 90 85 80 75 70 65 60 55 50

让我们再举一个例子:

0:pi/8:pi

MATLAB执行该语句,并返回以下结果:

ans =

Columns 1 through 7

0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562

Columns 8 through 9

2.7489 3.1416

可以使用冒号运算符来创建矢量指数选择行,列或数组中的元素。

下表描述了其用于此目的(让我们有一个矩阵A):

格式

目的

A(:,j)

is the jth column of A.

A(i,:)

is the ith row of A.

A(:,:)

is the equivalent two-dimensional array. For matrices this is the same as A.

A(j:k)

is A(j), A(j+1),...,A(k).

A(:,j:k)

is A(:,j), A(:,j+1),...,A(:,k).

A(:,:,k)

is the kth page of three-dimensional array A.

A(i,j,k,:)

is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.

A(:)

is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.

例子

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

A=[1234;4567;78910]A(:,2)%second column of A

A(:,2:3)%secondandthird column of A

A(2:3,2:3)%secondandthird rowsandsecondandthird columns

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

A =

1 2 3 4

4 5 6 7

7 8 9 10

ans =

2

5

8

ans =

2 3

5 6

8 9

ans =

5 6

8 9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值