Fastest way to have a matrix with intersected elements only between matrix and array

Hello,
Lets say I have the following matrix:
0.1 0.2 0.3
0.3 0.5 0.7
0.8 0.1 0.9
And the following array:
0.1 0.3 0.5
Id like know the fastest way to get to the following result:
0.1 0 0.3
0.3 0.5 0.7
0 0 0
So far Ive achieved it with the following, which creates a mask and multiplys it:
idxs = ismembc( domainWeight, dWeightVector );
dWeightVector = bsxfun(@times, idxs, domainWeight);
Is there a fastest way to intersect a matrix with an array and return a matrix, with its original size, but only populated with the elements that exist on both original array and matrix?
Thank you

답변 (2개)

A=[0.1 0.2 0.3
0.3 0.5 0.7
0.8 0.1 0.9]
B=[0.1 0.5 0.3]
idx=ismember(A,B)
A(~idx)=0

댓글 수: 1

Although the last line in fact does give me some speedup since it gets rid of element-wise multiplication, ismember is still the bottleneck of this code.
Im trying to rewrite ismembc2.cpp (generating a mex file) in order to get unique entries and sort them

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

dpb
dpb 2016년 6월 6일
편집: dpb 2016년 6월 6일
Close
>> a.*ismember(a,v)
ans =
0.1000 0 0.3000
0.3000 0.5000 0
0 0.1000 0
>>
NB: It appears your answer isn't correct; 0.7 isn't included in v so 2nd row, 3rd column should be 0 but the 0.1 in row3, column 2, does match the 0.1 in the vector. Unless it were to be corresponding positions in the vector and the array row, but then the 0.3 wouldn't appear because while it's in both, it isn't in same column so that doesn't seem to be the rule, either.

댓글 수: 1

That actually happens because the array isnt sorted, which is one of the reasons why ismembc is faster than ismember, for it doesnt do some sanity checks. (My bad, should have sorted that out first)

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 6월 6일

댓글:

2016년 6월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by