Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25869 > unrolled thread

sorting a matrix based on values in an array

Started byharryos <oswald.harry@gmail.com>
First post2011-02-08 08:35 -0800
Last post2011-02-08 10:21 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  sorting a matrix based on values in an array harryos <oswald.harry@gmail.com> - 2011-02-08 08:35 -0800
    Re: sorting a matrix based on values in an array markspace <nospam@nowhere.com> - 2011-02-08 10:21 -0800

#25869 — sorting a matrix based on values in an array

Fromharryos <oswald.harry@gmail.com>
Date2011-02-08 08:35 -0800
Subjectsorting a matrix based on values in an array
Message-ID<6e7c1410-2a14-4ddc-b2e0-0d248533bdc6@q40g2000prh.googlegroups.com>
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

[toc] | [next] | [standalone]


#25870

Frommarkspace <nospam@nowhere.com>
Date2011-02-08 10:21 -0800
Message-ID<iis1ij$6pc$1@news.eternal-september.org>
In reply to#25869
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).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web