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

4 Ansichten (letzte 30 Tage)
EvaUCL
EvaUCL am 2 Aug. 2015
Bearbeitet: Jan am 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 2 Aug. 2015
Bearbeitet: Jan am 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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by