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?
% Given:
rowV1 = randi(1000,1,100);
rowV2 = randi(1000,1,100);
colV1 = rowV1';
colV2 = rowV2';
% Perform:
% 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.
% Results:
  댓글 수: 1
Stephen23 2017년 2월 16일
편집: Stephen23 2017년 2월 16일
Get rid of the conjugate transpose, otherwise you are comparing apples with oranges.

댓글을 달려면 로그인하십시오.

채택된 답변

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.

추가 답변(1개)

Stephen23 2017년 2월 16일
I think the answer is "the one that make the code easiest to understand".
Why is this the answer? Because:
  1. commands like this are unlikely to be a major bottleneck of your code, and
  2. 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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by