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

5 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by