简体   繁体   English

如何在Matlab中找到矩阵中一个向量的元素的索引?

[英]How do I find the indices of the elements of one vector in a matrix in Matlab?

Suppose I have a 9x9 matrix A that that consists of integers. 假设我有一个9x9的矩阵A,它由整数组成。 I have another matrix IDX that's 2500x4 and consists of the same integers in A. I want to find the indices of all the values in IDX in the matrix A. 我有另一个2500x4矩阵IDX,它由A中相同的整数组成。我想找到矩阵A中IDX中所有值的索引。

Here's what I have: 这是我所拥有的:

for ii=1:length(IDX)
     Mat_idx=ismember(A,IDX(ii,:));
     [StatIdxX StatIdxY] = find(Mat_idx);
end

Now for each ii the StatIdxX and StatIdxY are the row and col indices of IDX in the matrix A. This is slow, and the culprit is ismember 现在,对于每个ii,StatIdxX和StatIdxY是矩阵A中IDX的行索引和col索引。这很慢,而罪魁祸首是ismember

Any thoughts on speeding this up? 有什么想法可以加快速度吗?

Thanks. 谢谢。

first flatten A with A=A(:) , that will make a single linear index instead of row,col. 首先使用A=A(:)展平A ,这将形成单个线性索引,而不是row,col。 Then just use logical indexing. 然后,只需使用逻辑索引即可。 For example: 例如:

B=zeros(size(IDX));
for n=1:numel(A)
B(IDX==A(n))=n;
end

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

相关问题 如何在MATLAB中的向量中删除一组索引处的元素? - How do I remove elements at a set of indices in a vector in MATLAB? 如何在不排序的情况下找到向量中2个最小元素的索引? - How can I find the indices of the 2 smallest elements in a vector without sorting? 如何在MATLAB中找到向量的分段索引? - How to find segment indices of vector in MATLAB? Matlab:如何在向量中查找特定值的索引 - Matlab: How to find indices of a specific value in vector 如何在Matlab中找到> 0的矩阵元素? - How to find the elements of a matrix > 0 in Matlab? 如何在Matlab中找到具有n个公共元素的大行矩阵的行索引? - How to find row indices of large row matrix with n common elements in matlab? 如何将给定矩阵每一行中的所有元素与给定向量的相应元素相乘,然后在MATLAB中求和? - How do I multiply all the elements in each row of a given matrix with corresponding elements of a given vector and sum them in MATLAB? 如何在Matlab中更改矩阵的索引? - How can i change indices of a matrix in Matlab? 如何找到矩阵中与值K元素相邻的所有值R的元素? -Matlab - How do i find all elements of value R in a matrix that are neighboring an element of value K? - Matlab 如何在 Matlab 中将变量的矩阵和列向量相乘? - How do I multiply a matrix and a column vector of variables in Matlab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM