Can you optimize this row matching code?

조회 수: 1 (최근 30일)
Amir
Amir 2014년 4월 23일
편집: Amir 2014년 4월 23일
Hi everyone,
I have a huge matrix on which I need to do some matching operation. Here's the code I have written and it works fine, but I think there is some room to make it more optimized or write another code that does the matching in less time. Could you please help me with that?
rowsMatched = find(bigMatrix(:, 1) == matchingRow(1, 1) & bigMatrix(:, 2) == matchingRow(1, 2) & bigMatrix(:, 3) == matchingRow(1, 3))
The problem with this code is that I cannot use the && operand. So, in case one of the columns do not match the program still checks the next condition. How do you think I can avoid this?
Thank you
  댓글 수: 2
Matt Kindig
Matt Kindig 2014년 4월 23일
Is matchingRow only a 1x3 vector? Or do you loop through all rows in matchingRow?
Can you post your full code?
Amir
Amir 2014년 4월 23일
편집: Amir 2014년 4월 23일
It's just a 1x3 vector in this case, but it could be up to 1 x 10 (in case I had a bigMatrix with the size, say, (1m, 10). However, I still don't know how to make the code that much flexible being able to do the matching for a 1 x 3 up to 1 x 10 matchingRow vector.

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

채택된 답변

Amir
Amir 2014년 4월 23일
편집: Amir 2014년 4월 23일
Here's the solution:
rowsMatched = find(all(bsxfun(@eq, bigMatrix, matchingRow),2))

추가 답변 (1개)

Matt Kindig
Matt Kindig 2014년 4월 23일
How about this approach? Is it fast enough?
%bigMatrix is MxN, matchingRow is 1xN
rowsMatched = find(ismember( bigMatrix(:,1:3), matchingRow, 'rows'));

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by