Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 91 characters in body
Source Link
Dan
  • 45.8k
  • 20
  • 98
  • 170

Just to add to @ThijsW's answer, there is a significant speed advantage to the first method over the concatenation method:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004611 seconds. Elapsed time is 0.016448 seconds. Elapsed time is 0.034107 seconds. Elapsed time is 12.341434 seconds. 

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to more recent versions of Matlab

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004611 seconds. Elapsed time is 0.016448 seconds. Elapsed time is 0.034107 seconds. Elapsed time is 12.341434 seconds. 

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to more recent versions of Matlab

Just to add to @ThijsW's answer, there is a significant speed advantage to the first method over the concatenation method:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004611 seconds. Elapsed time is 0.016448 seconds. Elapsed time is 0.034107 seconds. Elapsed time is 12.341434 seconds. 

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to more recent versions of Matlab

added 255 characters in body
Source Link
Dan
  • 45.8k
  • 20
  • 98
  • 170

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc  Elapsed time is 0.004756004611 seconds.  Elapsed time is 0.017050016448 seconds.  Elapsed time is 0.048165034107 seconds.  Elapsed time is 1312.642334341434 seconds. 

It seems concatenation is significantly slower than writing-past-the-end, so you'll also haveI got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to consider that.more recent versions of Matlab

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004756 seconds. Elapsed time is 0.017050 seconds. Elapsed time is 0.048165 seconds. Elapsed time is 13.642334 seconds. 

It seems concatenation is significantly slower than writing-past-the-end, so you'll also have to consider that.

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc x = []; tic; for ii = 1:big x = [x rand]; end; toc  Elapsed time is 0.004611 seconds.  Elapsed time is 0.016448 seconds.  Elapsed time is 0.034107 seconds.  Elapsed time is 12.341434 seconds. 

I got these times running in 2012b however when I ran the same code on the same computer in matlab 2010a I get

Elapsed time is 0.003044 seconds. Elapsed time is 0.009947 seconds. Elapsed time is 12.013875 seconds. Elapsed time is 12.165593 seconds. 

So I guess the speed advantage only applies to more recent versions of Matlab

added 162 characters in body
Source Link
Rody Oldenhuis
  • 38.1k
  • 7
  • 53
  • 98

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:100000 big x(end+1) = 1;rand; end; toc;toc    x = []; tic; for ii = 1:100000 big x = [x 1];rand]; end; toc Elapsed time is 0.038783004756 seconds. Elapsed time is 120.134698017050 seconds. Elapsed time is 0.048165 seconds. Elapsed time is 13.642334 seconds. 

It looks like concatenatingseems concatenation is significantly slower which is somethingthan writing-past-the-end, so you'll also have to consider that.

Just to add to @ThijsW's answer:

x = []; tic; for ii = 1:100000  x(end+1) = 1; end; toc; x = []; tic; for ii = 1:100000  x = [x 1]; end; toc Elapsed time is 0.038783 seconds. Elapsed time is 12.134698 seconds. 

It looks like concatenating is significantly slower which is something to consider.

Just to add to @ThijsW's answer:

big = 1e5; tic; x = rand(big,1); toc x = zeros(big,1); tic; for ii = 1:big x(ii) = rand; end toc x = []; tic; for ii = 1:big x(end+1) = rand; end; toc    x = []; tic; for ii = 1:big x = [x rand]; end; toc Elapsed time is 0.004756 seconds. Elapsed time is 0.017050 seconds. Elapsed time is 0.048165 seconds. Elapsed time is 13.642334 seconds. 

It seems concatenation is significantly slower than writing-past-the-end, so you'll also have to consider that.

Source Link
Dan
  • 45.8k
  • 20
  • 98
  • 170
Loading