Results 1 to 7 of 7
- 08-05-2012, 09:35 AM #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)
- 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?
ThanksLast edited by Josh R; 08-05-2012 at 10:40 AM.
- 08-05-2012, 11:06 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Re: Rewriting MATLAB functions in Java
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++]; }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-05-2012, 01:08 PM #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]
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).
- 08-05-2012, 02:24 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-05-2012, 03:11 PM #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?
- 08-05-2012, 03:42 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,421
- Blog Entries
- 7
- Rep Power
- 26
Re: Rewriting MATLAB functions in Java
Not really; I wrote a blog article once here that may give you some ideas ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-07-2012, 03:17 PM #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.
Similar Threads
-
matlab - java
By henna in forum CLDC and MIDPReplies: 1Last Post: 02-04-2012, 07:58 AM -
How to apply a patch (Call Matlab from Java)
By Niroshan in forum Advanced JavaReplies: 6Last Post: 07-09-2011, 02:55 AM -
run a matlab script from java
By npoorni in forum Advanced JavaReplies: 5Last Post: 02-11-2010, 03:32 PM -
Help, Integrate a Java with Matlab
By toby in forum Advanced JavaReplies: 2Last Post: 08-07-2008, 07:08 AM -
I need to be able to deal with functions like matlab
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:37 AM
Bookmarks