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

5 vues (au cours des 30 derniers jours)
EvaUCL
EvaUCL le 2 Août 2015
Modifié(e) : Jan le 2 Août 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

Réponse acceptée

Image Analyst
Image Analyst le 2 Août 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 commentaire
Star Strider
Star Strider le 2 Août 2015
‘... create a row vector ...’
... and then transpose it to a column vector ...

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 2 Août 2015
Modifié(e) : Jan le 2 Août 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

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by