Thread: Rewriting MATLAB functions in Java

1. Member Join Date
Aug 2012
Posts
4
Rep Power
0 Rewriting MATLAB functions in Java

Hi All
I'm trying to rewrite a snippet of code I've written using Matlab into java and hoping to enlist the help of some experts.

The matlab code is;

Java Code:
A=[1,2,35,5,63,1.......] %some random vector dimension 1x100
B=reshape(A,10,10);      %takes vector A and reshapes it (column-wise) into a 10x10 matrix
C=B*B;                   %Multiplys the 10x10 matrix B by itself and saves it to C
D=[1,3
11,13];                  %creates a 2x2 matrix of integers
E=C(D);                  %returns a 2x2 matrix containing the 1st, 3rd,
...11th and 13th element of matrix C. Given this is
...a 10x10 matrix, this corresponds to the elements
...in position (1,1), (3,1), (1,2) & (3,2)
My main sticking points are;
- finding a function that can 'reshape' a vector into a matrix
- matrix multiplication for large matricies (atleast 10x10)
- extracting a smaller sub matrix from a larger matrix.

My current attempt to do this is using for-loops, but the execution time is incredibly slow compared to Matlab. Can anyone point me in the right direction of some java functions that can achieve the same/or similar effect without using for-loops?

Thanks
Last edited by Josh R; 08-05-2012 at 10:40 AM.  Reply With Quote

2. Re: Rewriting MATLAB functions in Java Originally Posted by Josh R My current attempt to do this is using for-loops, but the execution time is incredibly slow compared to Matlab. Can anyone point me in the right direction of some java functions that can achieve the same/or similar effect without using for-loops?
You can't do it without a bit of loops but I don't understand why it would be so slow; how fast runs this?

Java Code:
void reshape(double[][] a, double[] v) {

for (int p= 0, i= 0; i < a.length; i++)
for (int j= 0; j < a[i].length; j++)
if (p == v.length) return;
else a[i][j]= v[p++];
}
This little method copies the content of a vector v to a matrix a for as many elements in a or v (whichever is smaller).

kind regards,

Jos  Reply With Quote

3. Member Join Date
Aug 2012
Posts
4
Rep Power
0 Re: Rewriting MATLAB functions in Java

Hi Jos
Thanks for the code and your comments.

I tested it on a 100x100 matrix which executed 0.000304 seconds, however the native 'reshape' function in matlab can execute in 0.000010 seconds (~30x faster).

Admittedly, even the for loop approach is quite fast but this code will be executing many times per second so minimising execution time is my key focus.

Its my understanding that a matrix is stored as an array in the memory anyway i.e:

Java Code:
A = [1.3,1.1] is stored as [ 1.3 | 2.5 | 1.1 | 4.5 ]
[2.5,4.5]
Is there a way to just change the dimensions without having to rewrite the data?

To be honest, the slowest parts of the code are actually the;
- matrix multiplication (which i'm currently doing using for-loops, i feel like I'm overlooking a far simpler way)
- Extracting a smaller sub matrix from a larger matrix (particlarly when the the matricies get large).  Reply With Quote

4. Re: Rewriting MATLAB functions in Java

In C and C++ (and presumably in Matlab) matrixes are stored in memory as if they were just vectors; reshaping them doesn't cause any moving around of data; not so in Java where a matrix is not more than an array of arrays. The one dimensional (row)arrays do not need to be adjacent, so data needs to be hauled over. You can defer part of the carrying around to the System.arraycopy( ... ) method but I hink that all the boudary tests per row of the matrix slow down the process again (test it to be sure).

Multiplying two matrixes can be easily done the naive way; the Strassen 'fast' multiplication method has a lot of overhead, but again: test it to be sure.

If those matrixes are huge you might have a look at matrix 'views' where no data is moved around.

kind regards,

Jos  Reply With Quote

5. Member Join Date
Aug 2012
Posts
4
Rep Power
0 Re: Rewriting MATLAB functions in Java

That explains a lot. Thanks for the help.

Are you able to post a link to some documentation on these matrix 'views' you are referring?  Reply With Quote

6. Re: Rewriting MATLAB functions in Java Originally Posted by Josh R That explains a lot. Thanks for the help.

Are you able to post a link to some documentation on these matrix 'views' you are referring?
Not really; I wrote a blog article once here that may give you some ideas ...

kind regards,

Jos  Reply With Quote

7. Member Join Date
Aug 2012
Posts
4
Rep Power
0 Re: Rewriting MATLAB functions in Java

For anyone else raised in a MATLAB world and currently trying to learn Java... like me.

I recommend looking at the following library for matrix calcs;

efficient-java-matrix-library

Its the closest thing I've found to Matlab functionality so far, and avoids having to use so many for-loops.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•