필터 지우기
필터 지우기

Can this be vectorized?

조회 수: 2 (최근 30일)
Patrick Mboma
Patrick Mboma 2014년 10월 30일
댓글: Patrick Mboma 2014년 10월 30일
Dear all,
I have two vectors A and B of respective sizes na and nb, with na>=nb. All elements in B are unique while the elements in A are not. But all elements in A are in B. I would like to create a mapping between A and B and one way to do that is as follows:
A=nan(na,1);
for ii=1:nb
A(A==B(ii))=ii;
end
This works well be becomes prohibitively expensive for large na. And so my question is whether there is a way of getting rid of the for loop and/or creating this mapping differently.
Thanks,
P.
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2014년 10월 30일
can so:
out = nan(na,1);
for ii=1:nb
out(A==B(ii))=ii;
end

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 10월 30일
편집: Andrei Bobrov 2014년 10월 30일
[lc,ii] = ismember(A,B);
A(lc) = ii(lc);
or with nan
out = ii;
out(~lc) = nan;
or jast (with zeros)
out = ii;
  댓글 수: 1
Patrick Mboma
Patrick Mboma 2014년 10월 30일
Thanks Andrei,
I ended up implementing it as
[~,out] = ismember(A,B);
where out is exactly what I am looking for (given that all the elements in A are in B).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by