Obtaining all the indices of matached elements of one array in another

조회 수: 28 (최근 30일)
Mohammed Almatrafi
Mohammed Almatrafi 2017년 11월 1일
답변: Carl Witthoft 2019년 9월 27일
Given two vectors A and B, I'd like to find all the indices of matched elements of B in A. So if I have:
A=[1 2 1 2 2 3 1];
B=[1 2 3];
output=[1 3 7;2 4 5;6 0 0].
The first row corresponds to find(A==B(1)), and the second row corresponds to find(A==B(2)), etc;
I know this can be done easily with for loop, but looping over the vector B will be really slow as I have a really big vector of A and vector of B with 43200 elements. Moreover, I am doing a long process after this step, so I am looking for solution through vectorization. I tried different approaches, but non gave me all indices! I am not sure if there's solution through vectorization, but I thought I'd ask here.
Any ideas?

답변 (3개)

Eric
Eric 2017년 11월 1일
편집: Eric 2017년 11월 1일
You will run into problems if there are different amounts of A which equal B. If you care to know both indicies, use something like this:
[ia,ib] = find(bsxfun(@eq,shiftdim(A,1),B));
It will return all possible matches of all values of B to A, where the ib are the indicies of B and ia are the indicies in A. If you know for sure that there will always be 3 matches in all of A to every B, you can add something like
output = reshape(ia,[3 numel(ia)/3])';
to get your above output (confirmed with your provided test case of A and B).
  댓글 수: 1
Mohammed Almatrafi
Mohammed Almatrafi 2017년 11월 1일
편집: Mohammed Almatrafi 2017년 11월 1일
Thanks for your answer. there are different functions that can achieve the same ia and ib obtained by your function. the problem is that the number of repetitions of each element of B in A is different.
Thanks again.

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2017년 11월 1일
As Eric said, you will run into trouble in most cases. I suggest you store the output in a cell array, like this:
A = [1 2 1 1 2 3]
B = [1 2 3 4]
output = arrayfun(@(x) find(A==x), B, 'un', 0)
% output{k} holds all the indices into A, where A equals B(k)
  댓글 수: 2
Mohammed Almatrafi
Mohammed Almatrafi 2017년 11월 1일
편집: Mohammed Almatrafi 2017년 11월 1일
Thank you for your suggested function. However, it seems that it takes more time than it takes with for loop to obtain the desired result.
Elapsed time is 0.001405 seconds. // Time taken with your function
Elapsed time is 0.000040 seconds. // with for loop
thanks a lot for you contribution.
Jos (10584)
Jos (10584) 2017년 11월 2일
well written for-loops with pre-allocation are nowadays very fast. As you asked for a vectorised solution, I just provided one :)

댓글을 달려면 로그인하십시오.


Carl Witthoft
Carl Witthoft 2019년 9월 27일
If your version of MATLAB isn't too old, look up help on intersect, to wit;
[dat, idxa, idxb] = intersect(a,b,'stable')

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by