sorting a matrix based on values in an array

Discussion in 'Java' started by harryos, Feb 8, 2011.

  1. harryos

    harryos Guest

    hi,
    I have a 1D array of doubles each of which correspond to a row in a
    matrix(a 2D array).The 1D array is unsorted.I need to sort the data in
    the matrix based on the values of the 1D array.
    ie,
    double[] aVector = new double[]{0, 4.4521, 2.435, 3.654};
    double[][] aMatrix = new double[][]{
    {-0.142571, -0.692875, -0.064821, -0.567353},
    {0.585747, -0.30768, -0.465215, 0.44606},
    {-0.284163, -0.145485, 0.357779, 0.352733},
    {-0.290938, -0.103984, 0.373695, 0.319145}
    };
    0 of aVector corresponds to the row {-0.142571, -0.692875, -0.064821,
    -0.567353} of aMatrix.
    3.654 of aVector corresponds to the row {-0.290938, -0.103984,
    0.373695, 0.319145} etc..

    After sorting the arrays should look like;
    aVector = [4.4521, 3.654, 2.435, 0 ]
    aMatrix = [
    [0.585747, -0.30768, -0.465215, 0.44606 ],
    [ -0.290938, -0.103984, 0.373695, 0.319145 ],
    [-0.284163, -0.145485, 0.357779, 0.352733 ],
    [-0.142571, -0.692875, -0.064821, -0.567353],
    ]
    I thought of using a Hashtable<Double, double[]> and storing the
    elements of aVector as keys, and rows of aMatrix as values.This would
    let me sort the keys and finally build a sorted matrix from the
    hashtable.But then this approach would fail if aVector contains a
    duplicate element.
    aVector = [4.4521, 3.654, 3.654, 0 ]

    In this case, two rows of aMatrix correspond to the same value of
    3.654 from aVector.It would make it impossible to use a Hashtable..Is
    there any way I can get the sorting done in such a scenario?
    Any help,advice would be appreciated..
    thanks
    harry
    harryos, Feb 8, 2011
    #1
    1. Advertising

  2. harryos

    markspace Guest

    On 2/8/2011 8:35 AM, harryos wrote:

    > I thought of using a Hashtable<Double, double[]> and storing the
    > elements of aVector as keys, and rows of aMatrix as values.This would
    > let me sort the keys and finally build a sorted matrix from the
    > hashtable.But then this approach would fail if aVector contains a
    > duplicate element.
    > aVector = [4.4521, 3.654, 3.654, 0 ]



    Assuming this would otherwise work (I didn't read your spec carefully)
    Java has an IdentityHashMap that stores by object reference (i.e., the
    actual array objects themselves) rather than value or content of the
    object.

    So an identical row wouldn't matter, as long as you used a separate
    array to store the values in (almost certainly the case unless you are
    comparing each row and re-using an array if it's identical).
    markspace, Feb 8, 2011
    #2
    1. Advertising

  3. harryos

    Roedy Green Guest

    On Tue, 8 Feb 2011 08:35:27 -0800 (PST), harryos
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I have a 1D array of doubles each of which correspond to a row in a
    >matrix(a 2D array).The 1D array is unsorted.I need to sort the data in
    >the matrix based on the values of the 1D array.


    Write a custom comparator that has a constructor that takes a set of
    column numbers to sort.

    Have a look at the code for CSVSort. See
    http://mindprod.com/products1.html#CSV
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Your top priority should be fixing bugs. If you carry on development,
    you are just creating more places you will have to search for them.
    Roedy Green, Feb 11, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,651
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    403
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    544
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    584
    Robert Kern
    Apr 2, 2009
  5. Replies:
    5
    Views:
    151
    Randy Webb
    Jun 21, 2005
Loading...

Share This Page