Is it better to use the dimensional cat function(s) versus using brackets?
13 views (last 30 days)
Show older comments
I've timed the various methods of concatenating vectors and I'd like someone to interpret the results a little bit. Knowing that MATLAB stores data in column-major order, I have a few questions:
1. Why is horizontal concatenation faster than vertical?
2. Why aren't all of the like-concatenation functions (cat(1,...) and horzcat(...) and [...,...]) equally efficient? Using cat along dimension 1 is the fastest horizontal concatenation, however using brackets is the fastest vertical concatenation.
3. If I am concatenating large vectors many times, what is the optimal way for doing so?
% Given:
rowV1 = randi(1000,1,100);
rowV2 = randi(1000,1,100);
colV1 = rowV1';
colV2 = rowV2';
% Perform:
% Get concatenated column vector.
t1=zeros(1000000,1);t2=t1;t3=t1;t4=t1;t5=t1;t6=t1;t7=t1;t8=t1;t9=t1;
for i = 1:100000
% Test transposed horizontal concatenation of rows.
tic;cat(2,rowV1,rowV2)';t1(i)=toc;
tic;horzcat(rowV1,rowV2)';t2(i)=toc;
tic;[rowV1,rowV2]';t3(i)=toc;
% Test vertical concatenation of columns.
tic;cat(1,colV1,colV2);t4(i)=toc;
tic;vertcat(colV1,colV2);t5(i)=toc;
tic;[colV1;colV2];t6(i)=toc;
% Test vertical concatenation of transposed rows.
tic;cat(1,rowV1',rowV2');t7(i)=toc;
tic;vertcat(rowV1',rowV2');t8(i)=toc;
tic;[rowV1';rowV2'];t9(i)=toc;
end
% Results:
{'_____','T_horiz_mean','vert_mean','vert_T_mean';...
'cat',mean(t1),mean(t4),mean(t7);...
'hv',mean(t2),mean(t5),mean(t8);...
'[]',mean(t3),mean(t6),mean(t9);...
'_____','T_horiz_std','vert_std','vert_T_std';...
'cat',std(t1),std(t4),std(t7);...
'hv',std(t3),std(t5),std(t8);...
'[]',std(t3),std(t6),std(t9)}
1 Comment
Accepted Answer
Jan
on 16 Feb 2017
If you concatenate two column vectors horizontally:
a = rand(5, 1);
b = rand(5, 1);
c = [a, b]
the contiguos blocks of memory are joined, because Matlab stores the data in columnwise order. In opposite to this, the vertical concatenation:
a = rand(1, 5);
b = rand(1, 5);
c = [a; b]
creates the output by copying one element after the otehr from both vectors, which is less efficient.
These effects are much more important than the question if you use [,] or horzcat. As far as I remember, a profiling with -detail builtin revealed in older Matlab versions, that for [,] the function horzcat is called internally, but I could not reproduce this in R2016b.
For measuring run times prefer either timeit or call the operation in a loop inside the tic/toc to increase the accuracy.
If the brackets are some micro seconds faster or slower than the cat functions, this might change with the next Matlab version. Therefore I would not concentrate on this detail, but use the method, which is better to read: The total time to solve a problem includes the time for programming, debugging and maintenance of the code also. "Premature optimization" is a common pitfall and you find some tutorials, if you search for this term in the net.
0 Comments
More Answers (1)
Stephen23
on 16 Feb 2017
I think the answer is "the one that make the code easiest to understand".
Why is this the answer? Because:
- commands like this are unlikely to be a major bottleneck of your code, and
- the JIT engine and internal optimization can change between MATLAB versions, which means that your finely timed difference of one/two/ten percent might not be relevant at all on another computer or on another version of MATLAB.
There is little point in trying to optimize your way into a corner like that. Most of your time is spent writing/reading/debugging code, so if you really want to save time, use the command that makes your code clearest and the least obfuscated.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!