Message Boards Message Boards

0
|
4119 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to find the rows of a floating point matrix that match another?

Posted 10 years ago
POSTED BY: Jonatan Lehtonen
2 Replies

To being with, I'm renaming A to matA and B to matB. User defined expressions and objects usually being with a lower case letter to separate them from the ones that come pre-defined. Also this gives us more information about what the objects are.

matA = {{0, 0.5, 1, 1.5, 2}, {1, 1.5, 2, 2.5, 3}, {3, 4.5, 6, 5, 2}};
matB = {{4.5, 6}, {1.5, 2}};

Given a row of B how can I find a corresponding row in A?

The function (#[[{2, 3}]] == row &) returns True with the 2nd and 3rd element of the input are equal (==) to row. Note that Equal (==) is not exact. Two floating point numbers that are very close for example will be considered equal when using Equal (==).

findReplacementInA[row_] := SelectFirst[matA, #[[{2, 3}]] == row &]

findReplacementInA[{4.5, 6}]
{3, 4.5, 6, 5, 2}

How can I apply this operation on every row of B?

Map[findReplacementInA, matB] or findReplacementInA /@ matB

POSTED BY: Sean Clarke

Thank you very much, that's exactly what I needed!

POSTED BY: Jonatan Lehtonen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract