Tab delimited to NxN Matrix

Questions or comments concerning matrices, import/export filters, basic calculations and matrix visualization can be submitted here.

Tab delimited to NxN Matrix

Postby yogeshp08 » Sat Jul 24, 2010 7:28 pm

Hi,

I have a tab delimited file in the format
Code: Select all
A   B   0.5
A   C   0.75
B   D   0.2

I need to create a NxN matrix like
Code: Select all
       A      B      C       D
A      0     0.5    0.75   
B             0             0.2
C                    0
D                            0

Can I do it with UJMP? Can someone please guide me on how to go about this?
Thanks,

-Yogesh
yogeshp08
 
Posts: 2
Joined: Sat Jul 24, 2010 5:45 am

Re: Tab delimited to NxN Matrix

Postby arndt » Mon Jul 26, 2010 6:19 pm

Your file has a sparse matrix format, but slightly different from what UJMP can handle. Everything would be fine if your file was like this:
Code: Select all
    0   1   0.5
    0   2   0.75
    1   3   0.2


So you have to replace the labels with the numbers first:

Code: Select all
Matrix orig = MatrixFactory.importFromFile(new File("test.csv"), "\\t");

orig.replaceRegex(Ret.ORIG, "A", "0");
orig.replaceRegex(Ret.ORIG, "B", "1");
orig.replaceRegex(Ret.ORIG, "C", "2");
orig.replaceRegex(Ret.ORIG, "D", "3");

Matrix sparse = MatrixFactory.sparse(orig);
Holger
arndt
Site Admin
 
Posts: 168
Joined: Mon Feb 02, 2009 7:02 pm
Location: Munich, Germany

Re: Tab delimited to NxN Matrix

Postby yogeshp08 » Wed Jul 28, 2010 5:39 pm

Hi,

To be more specific, instead of A, B, C, D I have numerical IDs.
Code: Select all
12243251   1299876   0.5
32553218   8763421   0.75
98664321   5643287   0.2

With this I get an ArrayIndexOutOfBoundsException
Code: Select all
Row: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at org.ujmp.core.stringmatrix.impl.SimpleDenseStringMatrix2D.getString(SimpleDenseStringMatrix2D.java:79)
    at org.ujmp.core.stringmatrix.stub.AbstractDenseStringMatrix2D.getString(AbstractDenseStringMatrix2D.java:42)
    at org.ujmp.core.stringmatrix.stub.AbstractStringMatrix.getAsString(AbstractStringMatrix.java:58)
    at org.ujmp.core.stringmatrix.stub.AbstractStringMatrix.getAsDouble(AbstractStringMatrix.java:48)
    at org.ujmp.core.doublematrix.calculation.general.misc.Dense2Sparse.calc(Dense2Sparse.java:44)
    at org.ujmp.core.MatrixFactory.sparse(MatrixFactory.java:681)
    at Tab2NxNMatrix.transform(Tab2NxNMatrix.java:33)
    at Tab2NxNMatrix.main(Tab2NxNMatrix.java:27)

Also, how can I write to sparse matrix to a file (in a matrix form).

Thanks
yogeshp08
 
Posts: 2
Joined: Sat Jul 24, 2010 5:45 am

Re: Tab delimited to NxN Matrix

Postby arndt » Tue Aug 24, 2010 10:01 am

String s = "12243251\t1299876\t0.5\n";
s += "32553218\t8763421\t0.75\n";
s += "98664321\t5643287\t0.2\n";

org.ujmp.core.Matrix m = MatrixFactory.importFromString(FileFormat.CSV, s);

System.out.println(m);

org.ujmp.core.Matrix m2 = MatrixFactory.sparse(m);

System.out.println("rows: " + m2.getRowCount());
System.out.println("cols: " + m2.getColumnCount());

m2.exportToFile(FileFormat.SPARSECSV, new File("d:/testsparse.csv"));
Holger
arndt
Site Admin
 
Posts: 168
Joined: Mon Feb 02, 2009 7:02 pm
Location: Munich, Germany

Re: Tab delimited to NxN Matrix

Postby arndt » Tue Aug 24, 2010 10:03 am

Works well for me:

Code: Select all
String s = "12243251\t1299876\t0.5\n";
s += "32553218\t8763421\t0.75\n";
s += "98664321\t5643287\t0.2\n";

org.ujmp.core.Matrix m = MatrixFactory.importFromString(FileFormat.CSV, s);

System.out.println(m);

org.ujmp.core.Matrix m2 = MatrixFactory.sparse(m);

System.out.println("rows: " + m2.getRowCount());
System.out.println("cols: " + m2.getColumnCount());

m2.exportToFile(FileFormat.SPARSECSV, new File("d:/testsparse.csv"));


Could you post your code, so I can reproduce the error?
Holger
arndt
Site Admin
 
Posts: 168
Joined: Mon Feb 02, 2009 7:02 pm
Location: Munich, Germany


Return to Universal Java Matrix Package

Who is online

Users browsing this forum: No registered users and 1 guest

cron