Is there a reason why row vectors are default in Matlab?
12 views (last 30 days)
Why is the default vector a row vector in Matlab? I do not have problems coding, but I wonder if anybody knows why Matlab does this.
When I create a vector like 1:100, or loop variablename(i), Matlab will create a row vector i.e. add element in the second dimension. In essence, variable(i) == variable(1,i) when creating vectors. This is illogical and cumbersome for the following reasons:
- When I create vectors and view them in Matlab, having them in the first dimension makes it easy to scroll through the values. I can't scroll sideways.
- matrix indexation should be logical. If I give one index, it's only logical to index the first dimension i.e. create a column vector.
- taking length(size(vector)) gives the number of dimensions of a matrix, unless of course, for vectors, which are created with an added singleton-dimension.
- Conversely, converting a matrix into a vector with matrix(:) creates a column vector, but why not create all vectors in the first dimension by default?
Granted, it is easy to bypass, often by transposing [vector]' or generating it in loop using vector(i,1). I'm just curious if there is a good reason for it
John D'Errico on 29 Jun 2022
It just is. Around 40 years ago, someone made a choice. The choice was fairly arbitrary. But they made it. And once made, that choice was forever set in stone, because all future MATLAB codes would assume that orientation.
Is there a good reason? Well, a simple one may be enough: When you display a vector, you want it to take up less room in the command window, so as a row. Column vectors just waste a lot of lines and will be more difficult to read.
The arguments you make are entirely valid, even if not sufficient. I suppose you might make the argument that memory should be stored as rows FIRST, not down columns, to then be consistent. Not something I ever worry about, nor does it matter once you are accustomed to the choice.
More Answers (4)
Steven Lord on 30 Jun 2022
Both the "A History of MATLAB" article that Cleve Moler and Jack Little wrote that was published by the ACM (see this blog post) as well as the Historic MATLAB Users' Guide (this other blog post) mention that the colon, : operator produce a row vector, but not why it does. So I guess the real answer is:
Jan on 29 Jun 2022
Matlab was developped as "Matrix Laboratory". So the default type is a double matrix. If the focus has been set to vectors, it would have the name "Veclab".
This is implemented consistently, e.g. length(size(1)) is 2 also, wich is a [1 x 1] matrix.
The automatic vanishing of trailing singelton dimensions, except if it is 2nd one, might feel strange, but this has been the decision some decades ago.
I'm not sure, if this decision has a "good" reason.
Bruno Luong on 2 Jul 2022
Edited: Bruno Luong on 2 Jul 2022
Beside an historical reason to me it seems it related to for the for-loop is designed to work
For M a true matrix
for v = M
it will loop on column of M, meaning v is M(:,1) at the first iteration, ... M(:,end) the last. Which is desirable since v is a column vector, compatible with vector in linear algebra and also it is a contiguous in memory due to MATLAB major-column memory storage, so more efficient for the for-loop to run.
Therefore a row vector is desired so that when you do
for x = r
it is desired to have r as (1 x n) in size so that it can really loop n times. Therefore the default value of expression (a:b) is to create a vector as row vector, ready to be looped and can be insert directly to a loop index as following:
for x = a:b
This is my opinion, what the creators of MATLAB was thinking I have no idea.
If you change one of the above organization, the for-loop syntax will be much messy or not efficient.
NOTE that for long time MATLAB did not have nd-array.