MATLAB Answers

smabtahi
0

command in Matlab equivalent to "forall " in fortran

smabtahi
さんによって質問されました 2012 年 5 月 24 日
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 件のコメント

サインイン to comment.

タグ

2 件の回答

Daniel Shub
回答者: Daniel Shub
2012 年 5 月 24 日
 採用された回答

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 件のコメント

smabtahi
2012 年 5 月 24 日
Thanks Daniel and Jan
Then it seems that I have no other choise than vectorizing jmat_TM.
Daniel Shub
2012 年 5 月 24 日
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.

サインイン to comment.


Wayne King
回答者: Wayne King
2012 年 5 月 24 日

A = 1:10;
A = A.^2;

  2 件のコメント

smabtahi
2012 年 5 月 24 日
Thank you Wayne.
of course the power function in pre-defined in matlab for whole matrixes, but consider a user defined function, for instance this loops:
for id=1:10
for jmd=1:100;
[Jsen(id,jmd)]=jmat_TM(id,jmd);
end
end
and jmat_TM is a user-defined complicated function. I want to get rid of using those loops.
Jan
2012 年 5 月 24 日
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.

サインイン to comment.



Translated by