Why does linspace create row instead of column?
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Benjamin D'Anjou
am 23 Mai 2019
Bearbeitet: Walter Roberson
am 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 Kommentare
Walter Roberson
am 23 Mai 2019
c = 1:4
uses the colon operator, not the linspace function. linspace would look like,
c = linspace(1, 4, 4)
Akzeptierte Antwort
Walter Roberson
am 23 Mai 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 Kommentare
Steven Lord
am 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
am 28 Jul. 2022
Bearbeitet: Walter Roberson
am 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 ' )
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!