Velocity Reviews > Java > sorting a matrix based on values in an array

# sorting a matrix based on values in an array

harryos
Guest
Posts: n/a

 02-08-2011
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?
thanks
harry

markspace
Guest
Posts: n/a

 02-08-2011
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 ]

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).

Roedy Green
Guest
Posts: n/a

 02-11-2011
On Tue, 8 Feb 2011 08:35:27 -0800 (PST), harryos
<(E-Mail Removed)> 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
--
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.