Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Avoiding ForLoops (like in MATLAB)?

  1. #1
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Avoiding ForLoops (like in MATLAB)?

    I know that in some languages like MATLAB, it is more efficient to use matrices instead of for loops where possible.
    For instance say I am interested in calculating the following sum:
    28*1000+1056*100+898*10+65,

    then in MATLAB it would be more efficient to do the following:
    x = [28,1056,898,65];
    y =10.^[length(x)-1:-1:0];
    z = sum(x.*y);
    instead of running a for loop.
    Does a similar concept exist in Java?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Avoiding ForLoops (like in MATLAB)?

    Quote Originally Posted by azalea View Post
    Does a similar concept exist in Java?
    Java has (anonymous) local classes; does a similar concept exist in Matlab? Those are two different languages; why does everyone demand that Java should have everything?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: Avoiding ForLoops (like in MATLAB)?

    um, what do you mean by (anonymous) local classes?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Avoiding ForLoops (like in MATLAB)?

    Quote Originally Posted by azalea View Post
    um, what do you mean by (anonymous) local classes?
    His point was that Java has things that matlab doesn't, and vice versa.

    But I'm not even sure what you're asking, since I don't really know what the code is doing under the hood in matlab. One rule that I like is that premature optimization is evil. Don't sacrifice maintainability for some perceived gain in efficiency.
    azalea likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Avoiding ForLoops (like in MATLAB)?

    I've never used matlab, but my guess would be that it has some additional overhead associated with for loops, and therefore provides an alternative to them which avoids that overhead. Java, as far as I know, has no significant overhead associated with for loops, so there's no problem just using them.

  6. #6
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: Avoiding ForLoops (like in MATLAB)?

    I see. I actually thought that matlab maybe using some sort of method for parallel computation, which is quicker than a simple forloop over a multi-dimensional matrix (although my example is just over a 1D array). Anyway, it made me wonder whether the same holds for other languages ...

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Avoiding ForLoops (like in MATLAB)?

    Quote Originally Posted by azalea View Post
    um, what do you mean by (anonymous) local classes?
    Maybe in Matlab explicit loops are slower because Matlab has to interpret the whole shebang; implicit loops (like in dot producs for vectors) are much faster. In Java an explicit loop is as fast as can be:

    Java Code:
    double[] x = { 28,1056,898,65 };
    double sum= 0;
    int factor= 1000;
     
    for (int i= 0; i < x.length; i++, factor/= 10)
       sum+= factor*x[i];
    kind regards,

    Jos

    ps. and Java has much much more that Matlab doesn't have and vice versa.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: Avoiding ForLoops (like in MATLAB)?

    [QUOTE=JosAH;241231]Maybe in Matlab explicit loops are slower because Matlab has to interpret the whole shebang; implicit loops (like in dot producs for vectors) are much faster. In Java an explicit loop is as fast as can be:

    Java Code:
    double[] x = { 28,1056,898,65 };
    double sum= 0;
    int factor= 1000;
     
    for (int i= 0; i < x.length; i++, factor/= 10)
       sum+= factor*x[i];
    I see. Thanks.
    Yes, I know, which is the reason why I'm coding in Java :-)

Similar Threads

  1. Avoiding allocations
    By Skiller in forum New To Java
    Replies: 7
    Last Post: 02-02-2011, 01:26 PM
  2. Replies: 3
    Last Post: 07-01-2010, 09:39 PM
  3. avoiding memory over-consuming
    By itaipee in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 12:59 PM
  4. Avoiding refresh
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-25-2008, 10:01 AM
  5. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 10:08 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
  •