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?

Re: Avoiding ForLoops (like in MATLAB)?

Quote:

Originally Posted by

**azalea** 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

Re: Avoiding ForLoops (like in MATLAB)?

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

Re: Avoiding ForLoops (like in MATLAB)?

Quote:

Originally Posted by

**azalea** 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.

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.

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

Re: Avoiding ForLoops (like in MATLAB)?

Quote:

Originally Posted by

**azalea** 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:

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.

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:

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 :-)