Operation without for loop
Ältere Kommentare anzeigen
I have two matrix.MAtrix A(250000x4)and MAtrix B(250000x4)
I have a function which result a scalar C.
C = myfunction(X,Y);
Now i want to pass each row of MAtrixA and MAtrixB to myfunction without using a for loop.
How to do it ?
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 31 Jan. 2012
0 Stimmen
You can make it look prettier using arrayfun(), but arrayfun() is just going to be doing the "for" loop internally.
4 Kommentare
Sukuchha
am 31 Jan. 2012
Walter Roberson
am 31 Jan. 2012
I would not expect arrayfun() to be faster than a "for" loop. Your initial question was, however, not about performance; it was about applying the operation without using a "for" loop.
Sukuchha
am 31 Jan. 2012
Walter Roberson
am 31 Jan. 2012
Often in such cases the best thing to do is rewrite the function so that it accepts arrays.
Note that "for" loops are not necessarily slow, especially in new versions. There is probably more overhead in calling myfunction all those times than there is in the "for" loop itself.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!