linearspace matlab,matlab常用函数

本文详细介绍了MATLAB中用于矩阵索引转换的ind2sub和sub2ind函数。ind2sub将线性索引转换为对应的行和列下标,而sub2ind则实现从行和列下标到线性索引的转换。通过示例,展示了这两个函数如何在矩阵操作中发挥作用,帮助理解MATLAB中矩阵索引的处理方式。
摘要由CSDN通过智能技术生成

ind2sub函数可以用来把矩阵元素的index转换成对应的下标(determines the equivalent subscript values corresponding to a single index into an array)

例如: 一个4*5的矩阵A,第2行第2个元素的index的6(matlab中matrix是按列顺序排列),可以用ind2sub函数来计算这个元素的下标 [I,J] = ind2sub(size(A),6)

[I,J] = ind2sub(siz,IND) returns the matrices I and J containing the equivalent row and column subscripts corresponding to each linear index in the matrixIND for a matrix of size siz. siz is a vector with ndim(A) elements (in this case, 2), wheresiz(1) is the number of rows andsiz(2) is the number of columns.

例子:For matrices, [I,J] = ind2sub(size(A),find(A>5)) returns the same values as[I,J] = find(A>5).

在命令窗口输入:

>> A=[4 7 2 9 8;3 9 1 4 3;1 5 9 6 4;8 3 7 1 0]

A =

4 7 2 9 8

3 9 1 4 3

1 5 9 6 4

8 3 7 1 0

则A中每个元素对应的索引如下(MATLAB中数据是按列的方式存储的):

1 5 9 13 17

2 6 10 14 18

3 7 11 15 19

4 8 12 16 20

ind=sub2ind(siz,I,J):siz表示要转换的矩阵的行列数,I是要转换矩阵的行标,J是要转换矩阵的列标。I,J的行列数必须相同。ind为输出参数,其行列数与I,J相同。ind即为索引。

在命令窗口中输入:

>> ind=sub2ind(size(A),[1,2;3,4],[1,1;2,2])

则显示

ind =

1 2

7 8

从运算结果可以看出,在矩阵A中,下标(1,1)的索引值为1,下标(2,1)的索引值为2,下标(3,2)的索引值为7,下标(4,2)的索引值为8

Like this:

Like Loading...

Related

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值