Borrar filtros
Borrar filtros

vectorization examples for efficiency

3 visualizaciones (últimos 30 días)
feynman feynman
feynman feynman el 10 de Mzo. de 2024
Comentada: feynman feynman el 11 de Mzo. de 2024
I wonder if the following expressions are already optimized forms for computational efficiency and if not how to rewrite them?
x=linspace(0,1);
0*x; x+1
a=[x;x]
[2*x;a(1,:)]
repmat([1;1;0],1,10);

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Mzo. de 2024
x = 0:1/99:1;
zeros(size(x)); x+1
a=[x;x];
[2*x; x]
repmat([1;1;0],1,10);
  3 comentarios
Walter Roberson
Walter Roberson el 11 de Mzo. de 2024
Editada: Walter Roberson el 11 de Mzo. de 2024
Interesting
When I try it several times, the times vary pretty wildly, including cases where the 0*x comes out much slower.
format long g
testit();
testit();
T = testit()
T = 3×1
0.000366 0.001324 0.000718
function T = testit()
T = zeros(3,1);
N = 10000;
x = linspace(0,1);
start = tic; for K = 1:N; Z = 0*x; end; T(1) = toc(start);
start = tic; for K = 1:N; Z = zeros(size(x)); end; T(2) = toc(start);
start = tic; for K = 1:N; Z = zeros(1,100); end; T(3) = toc(start);
end
feynman feynman
feynman feynman el 11 de Mzo. de 2024
, which means 0*x and zeros(size(x)) aren't necessarily better or worse than the other?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by