在 Matlab 中挑选给定指定列的矩阵条目,无需 for 循环
可能的重复:
MATLAB 索引问题
如何在Matlab中从矩阵中提取非垂直列< /a>
我觉得应该有一个简单的方法来做我想做的事,但我无法弄清楚这一点。
输入:一个 nx t
实数矩阵 M
和一个 nx 1
向量 I
索引
OUTPUT:一个 nx 1
向量 P
,使得 P(i) = M( i, I(i) )< /code>
很明显如何使用for
循环,但这是 Matlab,并且 n
很大。有没有办法向量化这个问题并避免 for
循环?
Possible Duplicate:
MATLAB indexing question
How to extract non-vertical column from matrix in Matlab
I feel like there should be a simple way to do what I want, but I cannot figure this out.
INPUT: an n x t
matrix M
of reals and an n x 1
vector I
of indices
OUTPUT: an n x 1
vector P
such that P(i) = M( i, I(i) )
It's obvious how to do this with a for
loop, but this is Matlab and n
is large. Is there a way to vectorize this problem and avoid the for
loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用线性索引的简单、快速、矢量化解决方案。
例子:
Here's a simple, fast, vectorized solution using linear indexing.
Example: