You can use BSXFUN to do it in a vectorized manner:
rowsMatched = find(all(bsxfun(@eq, bigMatrix, matchingRow),2));
Notice that it will work for any number of columns, just matchingRow
should have the same number of columns as bigMatrix
.
题
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 &&
operand. So, in case one of the columns do not match, the program still checks the next condition. How can I avoid this?
Update: Here's the solution to this problem:
rowsMatched = find(all(bsxfun(@eq, bigMatrix, matchingRow),2));
Thank you
解决方案
You can use BSXFUN to do it in a vectorized manner:
rowsMatched = find(all(bsxfun(@eq, bigMatrix, matchingRow),2));
Notice that it will work for any number of columns, just matchingRow
should have the same number of columns as bigMatrix
.