v(n:1)=1 where n>1
Mostrar comentarios más antiguos
Hello! I have some old code from a colleague who is unavailable to answer Qs. There is a case where:
v(n:1)=1 where n>1
The fact that n>1 is the part that is confusing me. I understand that the : acts as an indices range assigning the value 1 to all values in vector v.
What am I missing? Why would 1 be at the end? Does this reverse the order of the array or continue back to the first element after reaching the end?
Thanks!
1 comentario
Image Analyst
el 15 de Ag. de 2014
Please copy and paste the exact line of code. I don't understand the "where n>1" part. How are you determining that from the code? Or was it in a comment? Or do you have a for loop like "for k = 1 : n" and then the "v" line of code inside the for loop? Don't make us guess.
Respuestas (1)
Matlab doesn't reverse the order. Try this code:
A=1:10;
n=3;
A(n:1)=1
or run this:
3:1
answer is:
ans =
Empty matrix: 1-by-0
9 comentarios
AnnaB
el 15 de Ag. de 2014
AnnaB
el 15 de Ag. de 2014
AnnaB
el 15 de Ag. de 2014
Amir
el 15 de Ag. de 2014
AnnaB, when does it give 1 from n to end? v(n:m)? or v(n:end)?
Amir
el 15 de Ag. de 2014
Are you sure that the value of n has not been changed before this was run?
AnnaB
el 15 de Ag. de 2014
Editada: Image Analyst
el 15 de Ag. de 2014
Andy L
el 15 de Ag. de 2014
just swap n:1 with 1:n if that is the range that you want to make == 1, i.e
v(n:1) == 1; % becomes...
v(1:n) == 1;
Julia
el 15 de Ag. de 2014
From the Matlab help:
j:k is the same as [j,j+1,j+2,...,j+m], where m = fix(k-j). In the case where both j and k are integers, this is simply [j,j+1,...,k]. This syntax returns an empty matrix when j > k
So I think your guess that nothing happens is right.
Amir
el 15 de Ag. de 2014
I will ask this from my friends. If you find the reason please share it here.
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!