Results 1 to 7 of 7
  1. #1
    Josh R is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Rewriting MATLAB functions in Java

    Quote Originally Posted by Josh R View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Josh R is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Josh R is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Rewriting MATLAB functions in Java

    Quote Originally Posted by Josh R View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Josh R is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default 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

  1. matlab - java
    By henna in forum CLDC and MIDP
    Replies: 1
    Last Post: 02-04-2012, 07:58 AM
  2. How to apply a patch (Call Matlab from Java)
    By Niroshan in forum Advanced Java
    Replies: 6
    Last Post: 07-09-2011, 02:55 AM
  3. run a matlab script from java
    By npoorni in forum Advanced Java
    Replies: 5
    Last Post: 02-11-2010, 03:32 PM
  4. Help, Integrate a Java with Matlab
    By toby in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2008, 07:08 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:37 AM

Posting Permissions

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