필터 지우기
필터 지우기

Why does linspace create row instead of column?

조회 수: 12 (최근 30일)
Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 23일
편집: Walter Roberson 2022년 7월 28일
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
Walter Roberson
Walter Roberson 2019년 5월 23일
c = 1:4
uses the colon operator, not the linspace function. linspace would look like,
c = linspace(1, 4, 4)
Benjamin D'Anjou
Benjamin D'Anjou 2019년 5월 23일
I thought they were identical, but I was mistaken.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 5월 23일
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
Steven Lord
Steven Lord 2022년 7월 27일
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
Walter Roberson
Walter Roberson 2022년 7월 28일
편집: Walter Roberson 2022년 7월 28일
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 ' )

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by