Is it better to use the dimensional cat function(s) versus using brackets?
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
More Answers (1)
Stephen23
on 16 Feb 2017
2 votes
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.
Categories
Find more on Function Creation 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!