From: "Steven_Lord" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Speed difference between row and column vectors
Date: Thu, 18 Apr 2013 11:31:26 -0400
Organization: MathWorks
Lines: 73
Message-ID: <kkp3kd$gfd$>
References: <kkp1tf$au2$>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
X-Trace: 1366299085 16877 (18 Apr 2013 15:31:25 GMT)
NNTP-Posting-Date: Thu, 18 Apr 2013 15:31:25 +0000 (UTC)
In-Reply-To: <kkp1tf$au2$>
X-Priority: 3
X-MSMail-Priority: Normal
Importance: Normal
X-Newsreader: Microsoft Windows Live Mail 14.0.8089.726
X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8089.726
Xref: comp.soft-sys.matlab:793832

"Joel " <> wrote in message 
> Can anyone tell what's going on here? I have a hard time believing MATLAB 
> is so much slower when building column vectors.
>>> tic; for i=1:1e6; x=5.*[1, 2, 3]; end; toc
> Elapsed time is 0.389357 seconds.
>>> tic; for i=1:1e6; x=5.*([1, 2, 3]'); end; toc
> Elapsed time is 0.620597 seconds.
>>> tic; for i=1:1e6; x=5.*[1; 2; 3]; end; toc
> Elapsed time is 8.928639 seconds.
> They delay seems to not be in the multiplication operation, because in the 
> second example, the multiplication happens after the transpose.
> Thoughts?

1) What version of MATLAB are you using?
2) What operating system are you using?
3) Are you doing this in a function, in a script, or at the command prompt?
4) What else was going on when that third loop ran? Was some other 
application chewing up your system memory, requiring the OS to spend time 
swapping that application? Was something "phoning home" and again, using up 
your system's processing power for a little bit? My suspicion is that 
something started during the last 10% or so of the second loop's execution 
(causing a small slowdown) and continued through the entirety of the third 
loop's execution (causing a large slowdown.)

I was not able to reproduce these results using a function in release R2013a 
on my 64-bit Windows machine. All the elements of mt from the code below 
were fairly close to one another.

T = zeros(10, 3);
for k = 1:10
    T(k, :) = colvec_test;
mt = mean(T)


function T = colvec_test
T = zeros(1, 3);
for i=1:1e6;
    x=5.*[1, 2, 3];
tx = toc;
T(1) = tx;

for i=1:1e6;
    x=5.*([1, 2, 3]');
tx = toc;
T(2) = tx;

for i=1:1e6;
    x=5.*[1; 2; 3];
tx = toc;
T(3) = tx;

Steve Lord
To contact Technical Support use the Contact Us link on