Why does linspace create row instead of column?

In Matlab, array indexing is column based. For instance
A = [1,2;3,4]; % My matrix A
b = A(:); % This is a column vector b = [1;2;3;4]
So I was wondering if there is a reason why the linspace function creates a row vector
c = 1:4; % The vector is c = [1,2,3,4], a row vector
Is there a rationale in Matlab for this? Why doesn't linspace create a column by default, if that's the natural way to index things in Matlab?
I know this has little practical import, but it's bugging me that it was designed that way.
Edit: I thought linspace() and the semicolon operator were identical in that context. That appears not to be true. Either, it does not change the question.

2 Comments

c = 1:4
uses the colon operator, not the linspace function. linspace would look like,
c = linspace(1, 4, 4)
I thought they were identical, but I was mistaken.

Sign in to comment.

 Accepted Answer

The main reason is that row vectors take less screen space to print out -- easier to read, and less print-out (remember, MATLAB is old enough that it was common for people to use it with printing terminals.)
Do not confuse the shape of the vector with the location it indexes.
The shape of the result of indexing depends upon the shape of what is being indexed and the shape of the index. In the circumstance that what is being indexed is a vector, and the index is a vector, then the result is the same orientation as what is being indexed, not the same orientation as the index. In all other circumstances, the shape of the result is the same as the shape of the index.

3 Comments

The default linspace shape (row vector) has always frustrated me. Obviously it is consistent with colon array generation, but as a dedicated function it seems like it should have optional arguments to specify dimension, like with zeros/ones/etc in MATLAB, or with the axis argument numpy's linspace.
If the user is expected to use reshape/repmat commands for linspace, shouldn't zeros/ones also return row vectors?
Of course, a custom rewrite is all of three lines, but that's my two cents.
shouldn't zeros/ones also return row vectors
They can. They can also return column vectors, matrices, or N-dimensional arrays.
r = zeros(1, 5)
r = 1×5
0 0 0 0 0
isrow(r) % true
ans = logical
1
c = ones(5, 1)
c = 5×1
1 1 1 1 1
iscolumn(c) % true
ans = logical
1
linspace(start, stop, number).'
would be a column vector. The rewrite is all of 2 characters. Or use ' instead of .' for vectors that are guaranteed to be real valued. (I prefer the dotted version myself, as I have seen too many people encounter problems with ' )

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!