Re: matrix problem
- To: mathgroup at smc.vnet.net
- Subject: [mg107130] Re: matrix problem
- From: Raffy <raffy at mac.com>
- Date: Wed, 3 Feb 2010 06:12:35 -0500 (EST)
- References: <hk93ca$f68$1@smc.vnet.net>
On Feb 2, 3:47 am, Piotr Munik <piotr.mu... at gmail.com> wrote:
> Dear Math Group,
>
> I have matrix M and matrix N
> for example
> M={{1,2},{3,4},{5,6},{7,8}}
> N={{1,8},{5,0},{9,7}}
> (dimension is diffrent)
> I need to make new matrixes which include only this rows from matrix M and
> matrix N for which firsts elements are the same in both matrix (M and N).
> so I need
> M1={{1,2},{5,6}}
> N1={{1,8},{5,0}}
> Elements of matrix are real (not always integer)
> Please for help
> Peter
I could answer this question better if it was constrained a bit more:
-- Does the order of the first elements matter?
-- How are duplicate first elements handled?
Preserving order and ignoring duplicates:
mush[m_] := With[{common =
Cases[Tally@Flatten[DeleteDuplicates /@ m[[All, All, 1]]],
{x_, Length[m]} :> x, {1}]},
Transpose@{common, common /. Rule @@@ #} & /@ m];
mA = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
mB = {{1, 8}, {5, 0}, {9, 7}};
{mAA, mBB} = mush@{mA, mB}
mAA === {{1, 2}, {5, 6}}
mBB === {{1, 8}, {5, 0}}