在不使用sub2ind的情况下从矩阵中选择向量 [英] Selecting a vector from a matrix without using `sub2ind`

查看:61
本文介绍了在不使用sub2ind的情况下从矩阵中选择向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个5 x 5的矩阵M和两个向量

I have a 5 x 5 matrix M and two vectors

x=[1:5]
y=[1 4 3 5 2]

我想提取带有下标(x,y)的M的元素,即(1,1),(2,4),(3,3),(4,5),(5,2) .当然,我可以做类似

I would like to extract the elements of M with subscripts (x,y), i.e. (1,1),(2,4),(3,3),(4,5),(5,2). Of course, I could do something like

M(sub2ind([5,5],x,y))

但是,转换为索引会带来一些开销.还有另一种方法可以做到这一点吗?

But there is some overhead associated with the conversion to indices. Is there another way to do the same?

推荐答案

MATLAB使用列主格式,为什么不利用该格式呢? M是5x5 所以, 第一列是M(1),M(2),M(3),M(4),M(5). 因此,概括一下:

MATLAB uses Column Major format, why not exploit that? M is 5x5 So, The first column is M(1), M(2), M(3), M(4), M(5). So, Generalizing this:

M(x,y)= M((x +(y-1)* 5)

M(x,y) = M((x+(y-1)*5)

这篇关于在不使用sub2ind的情况下从矩阵中选择向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆