what's the n -space- 1 mean here?

조회 수: 2 (최근 30일)
EvaUCL
EvaUCL 2015년 8월 2일
편집: Jan 2015년 8월 2일
i'm learning about Matlab function now. this is one of the official answers of a course. i know it's very basic. but i'm confuse about it. here is the code..
function v = int_col(n)
v = [n 1:n-1]';
end

채택된 답변

Image Analyst
Image Analyst 2015년 8월 2일
If n = 4, then it's saying create a row vector where 4 is the first value and it's followed by 1 to n-1, in other words, followed by [1,2,3]. So the final vector would be [4, 1, 2, 3]. MATLAB style guidelines say that there should be a comma rather than a space, and sometimes (like if you did a Code Analyzer report and the row vector is the output of a function) you'll get an orange squiggle if you don't have it.
  댓글 수: 1
Star Strider
Star Strider 2015년 8월 2일
‘... create a row vector ...’
... and then transpose it to a column vector ...

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

추가 답변 (1개)

Jan
Jan 2015년 8월 2일
편집: Jan 2015년 8월 2일
The space is a smart way for a neater notation. It would be clearer to insert the (optional) comma:
v = [n, 1:n-1]';
Then [a, b] means, that the vectors a and b are concatenated horizontally. Matlab allows to omit the comma, but this can confuse the reader (e.g. you). It is not trivial to understand, what exactly happens in these cases:
[1 2]
[1 -2]
[1 - 2]
[1 -...
2]
[1 -...
2]
[1 ...
-2]
[1
-2]
Therefore I insert a comma and a semicolon where ever this is allowed and meaningful. Omitting it might save 0.1 seconds during typing and demand for minutes during debugging. I prefer:
[1, -2]
[1; ...
-2]
In addition I liek to insert spaces around operators and avoid a decimal dot without leading or trailing number. See:
.2.^.2 % Correct
0.2 .^ 0.2 % Less confusing

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by