# Is there a reason why row vectors are default in Matlab?

12 views (last 30 days)
Jonatan Tidare on 29 Jun 2022
Commented: Bruno Luong on 7 Jul 2022
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:
1. 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.
2. 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.
3. 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.
4. 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
Adam Danz on 2 Jul 2022
Edited: Adam Danz on 2 Jul 2022
For those interested in this topic, also see discussion here:

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.
John D'Errico on 2 Jul 2022
Edited: John D'Errico on 2 Jul 2022
Thinking about it, I would add another reason why a column vector is the technically correct orientation:
ALL arrays in MATLAB have an infinite number of dimensions. But you only see the starting non-singleton dimensions displayed. So the size of a 2x3 array is listed as [2,3], NOT as [2,3,1,1,1,1,1,1,1,1,1,1,...]
So logically, I want a vector, which has only ONE non-singleton dimension to be a COLUMN vector, thus with size [n,1,1,1,1,1,...].
Of course this is not the case. It goes back to the very early versions of MATLAB, when ONLY 1-D and 2-D arrays existed. So we had only vectors and simple rectangular arrays back in the dark ages. It was only when they introduced multiple dimensional arrays that we saw those singleton trailing dimensions suddenly appear.
Another argument ifor column vectors is if V is a default shape column vector, then I would kind of prefer if the simple dot prooduct between V and itself should be
dot(V,V) == V' * V
And that happens when v is a column vector.
Anyway, while I think I could make a mildly convincing argument in these contexts, that vectors SHOULD be column vectors by default, I think the display argument is still the one that would hold over as most important. Row vectors would still win the final argument, even if I recognize that column vectors make some sense.
In fact, if I were to decide to completely re-write MATLAB from scratch, I would still go with row vectors as the default vector shape, for no other reason than the display one.
None of it matters in the end of course, because as @Steven Lord pointed out, Cleve wanted it that way.

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:
why(189)
Cleve wanted it that way.

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.
Jonatan Tidare on 30 Jun 2022
Thank you for your answer. With two MVPs stating that it was simply a choice made long ago, and much else has been implemented around it, I believe that is the answer. I've just been curious about it.

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
...
end
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
...
end
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
...
end
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.
Adam Danz on 2 Jul 2022
Edited: Walter Roberson on 2 Jul 2022

Jonatan Tidare on 7 Jul 2022
Bruno Luong on 7 Jul 2022
If you are still curious about for loop
for v = A
...
end
Guess what is v and how many time it loops if A is nd-array (m1 x m2 x ... mn). I don't think I ever use this feature.

### Community Treasure Hunt

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

Start Hunting!

Translated by