v(n:1)=1 where n>1

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
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.

Iniciar sesión para comentar.

Respuestas (1)

Amir
Amir el 15 de Ag. de 2014
Editada: Amir el 15 de Ag. de 2014

0 votos

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
AnnaB el 15 de Ag. de 2014
Thanks for the very quick response :) and self help way of approaching this. I noticed that nothing actually happens to A. So does that mean that code is flawed or am I missing something again?
AnnaB
AnnaB el 15 de Ag. de 2014
So basically when A(n,m)=1 where n>m nothing happens where as if n<m then 1 is assigned to elements n to m. I just want to be sure that I'm not missing a mathlab function that I didn't know about.
AnnaB
AnnaB el 15 de Ag. de 2014
Looking at the values for v in the matlab workspace, the code has actually populated the value '1' for positions n to end. This must be another way of writing v(n:end)=1. Im just guessing though.
Amir
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
Amir el 15 de Ag. de 2014
Are you sure that the value of n has not been changed before this was run?
AnnaB
AnnaB el 15 de Ag. de 2014
Editada: Image Analyst el 15 de Ag. de 2014
I haven't used (n:end) in the code. It has worked with v(n:m) where n>m. I'm not sure how it works. You could be right that it has been successfully run in the past. However, if that was the case then v(n:1) would return an error, right?
It is so strange, there are 4 instances of this in the code and I don't know how it works.
for n>1
v(n:1)=1
t2=t(n:1)
d2=d1(n:1)-r(n:1)
Andy L
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
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
Amir el 15 de Ag. de 2014
I will ask this from my friends. If you find the reason please share it here.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 15 de Ag. de 2014

Comentada:

el 15 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by