fast way of filing up a matrix with function calls
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
i have a 2-D matrix m(i,j). for each (i,j) some function f(i,j) has to be evaluated and that fills up the matrix m(i,j). presently i am using a loop for i=1:N for j=1:N mat(i,j) = f(i,j); to fill up the matrix. Is there a faster implementation of this piece of code.
Antworten (2)
Azzi Abdelmalek
am 21 Dez. 2012
Bearbeitet: Azzi Abdelmalek
am 21 Dez. 2012
M=rand(4); % your matrix
f=@(x) sin(x)*x % your function
out=arrayfun(@(x) f(x),M)
Or you can use operation element by elemment (Faster)
out=sin(M).*M
4 Kommentare
Azzi Abdelmalek
am 21 Dez. 2012
Bearbeitet: Azzi Abdelmalek
am 21 Dez. 2012
x_grid=1:5;
y_grid=1:4;
[M1,M2]=meshgrid(x_grid,y_grid)
Then calculate using M1 and M2
Siehe auch
Kategorien
Mehr zu Logical 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!