command in Matlab equivalent to "forall " in fortran
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi Is there any command in matlab that can simpilfy such loop:
for i=1:10 A(i)=i^2 end
as I know in fortran it can be written anr run very fast as FORALL(i=1,10) A(i)=i**2
I hope similar thing can be found in Matlab too.
0 Kommentare
Akzeptierte Antwort
Daniel Shub
am 24 Mai 2012
The JIT accelerator that was introduced 10 years ago has sped up loops substantially, it is an unfortunate myth that MATLAB loops are slow. Vectorization can speed up some loops, but it can also slow things down. The ability to use parfor loops can also speed up things (and allow scaling) that is not easy with vectorized code.
For you case of wanting a user-defined function to be vectorized, the easy answer is to go back to the function (jmat_TM) and modify it to handle vector inputs.
2 Kommentare
Daniel Shub
am 24 Mai 2012
Or you could just stick with your loop. There is no guarantee that vectorizing will speed things up, although it will make your code for jmat_TM much more MATLAB like.
Weitere Antworten (1)
Wayne King
am 24 Mai 2012
A = 1:10;
A = A.^2;
2 Kommentare
Jan
am 24 Mai 2012
You can omit the square brackest around the output here.
You need either the loops or rewrite the user defined function, that it accepts matrices as inputs. Therefore Wayne's suggestion is fine.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!