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

2 views (last 30 days)
EvaUCL
EvaUCL on 2 Aug 2015
Edited: Jan on 2 Aug 2015
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

Accepted Answer

Image Analyst
Image Analyst on 2 Aug 2015
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 Comment
Star Strider
Star Strider on 2 Aug 2015
‘... create a row vector ...’
... and then transpose it to a column vector ...

Sign in to comment.

More Answers (1)

Jan
Jan on 2 Aug 2015
Edited: Jan on 2 Aug 2015
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

Products

Community Treasure Hunt

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

Start Hunting!