[英]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.