Why does linspace create row instead of column?
13 views (last 30 days)
Show older comments
Benjamin D'Anjou
on 23 May 2019
Edited: Walter Roberson
on 28 Jul 2022
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
Walter Roberson
on 23 May 2019
c = 1:4
uses the colon operator, not the linspace function. linspace would look like,
c = linspace(1, 4, 4)
Accepted Answer
Walter Roberson
on 23 May 2019
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
Steven Lord
on 27 Jul 2022
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)
isrow(r) % true
c = ones(5, 1)
iscolumn(c) % true
Walter Roberson
on 28 Jul 2022
Edited: Walter Roberson
on 28 Jul 2022
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 ' )
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!