sorting a matrix based on values in an array

H

harryos

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
 
M

markspace

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

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top