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);

 채택된 답변

Walter Roberson
Walter Roberson 2024년 3월 10일

0 개 추천

x = 0:1/99:1;
zeros(size(x)); x+1
a=[x;x];
[2*x; x]
repmat([1;1;0],1,10);

댓글 수: 3

feynman feynman
feynman feynman 2024년 3월 11일
Thanks! Comparison of 0*x with zeros(size(x)) shows they are close and for 10000 loops the former becomes faster.
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 2024년 3월 11일
, which means 0*x and zeros(size(x)) aren't necessarily better or worse than the other?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by