Is it better to use the dimensional cat function(s) versus using brackets?
조회 수: 26(최근 30일)
표시 이전 댓글
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?
rowV1 = randi(1000,1,100);
rowV2 = randi(1000,1,100);
colV1 = rowV1';
colV2 = rowV2';
% Get concatenated column vector.
for i = 1:100000
% Test transposed horizontal concatenation of rows.
% Test vertical concatenation of columns.
% Test vertical concatenation of transposed rows.
댓글 수: 1
Jan 2017년 2월 16일
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.
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.
Find more on Matrix Indexing 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!