简体   繁体   English

在矩阵中查找指数的某些值

[英]Finding certain VALUES of indices in a matrix

I have two vectors which are of same length M and N. The values of the vectors represent the indices of another matrix A so that the corresponding indices in vector M and N make index pairs of A. 我有两个具有相同长度M和N的向量。向量的值表示另一个矩阵A的索引,因此向量M和N中的对应索引构成A的索引对。

For example I have matrices 例如,我有矩阵

M=[1 2 3 4] and N=[5 6 7 8] M=[1 2 3 4]N=[5 6 7 8]

I would like to find the values of specific indices in matrix A and store them to another vector I, like this: 我想在矩阵A中找到特定索引的值并将它们存储到另一个向量I,如下所示:

I = [A(1,5) A(2,6) A(3,7) A(4,8)]

How could this be done? 怎么可以这样做?

You can convert them to linear indices using sub2ind and then use those linear indices to index A : 您可以使用sub2ind将它们转换为线性索引 ,然后使用这些线性索引来索引A

ind = sub2ind(size(A), M(:), N(:));
I = A(ind);

Note I've gone M(:) as this guarantees that M will be a column vector 注意我已经去了M(:)因为这可以保证M将是一个列向量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM