How to vectorize this "for" loop, or parallel it.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
fun = @(x,y) x + x.*y;
N = 1e3;
K = zeros(N);
for i = 1:N
for j = i:N
K(i,j) = fun(i, j);
end
end
0 Kommentare
Antworten (2)
Azzi Abdelmalek
am 23 Jan. 2014
Bearbeitet: Azzi Abdelmalek
am 23 Jan. 2014
fun = @(x,y) x + x.*y;
N=1000
[y,x]=meshgrid(1:N,1:N);
out=triu(fun(x,y))
2 Kommentare
Azzi Abdelmalek
am 23 Jan. 2014
Bearbeitet: Azzi Abdelmalek
am 23 Jan. 2014
N=4000;
tic
fun = @(x,y) x + x.*y;
[y,x]=meshgrid(1:N,1:N);
f=triu(fun(x,y));
toc
tic
K = zeros(N);
for i = 1:N
for j = i:N
K(i,j) = fun(i, j);
end
end
toc
isequal(f,K)
Elapsed time is 0.193062 seconds.
Elapsed time is 2.421795 seconds.
Siehe auch
Kategorien
Mehr zu Parallel Computing Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!