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

Image Analyst
Image Analyst 2014년 8월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Amir
Amir 2014년 8월 15일
편집: Amir 2014년 8월 15일

0 개 추천

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

AnnaB
AnnaB 2014년 8월 15일
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 2014년 8월 15일
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 2014년 8월 15일
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 2014년 8월 15일
AnnaB, when does it give 1 from n to end? v(n:m)? or v(n:end)?
Amir
Amir 2014년 8월 15일
Are you sure that the value of n has not been changed before this was run?
AnnaB
AnnaB 2014년 8월 15일
편집: Image Analyst 2014년 8월 15일
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 2014년 8월 15일
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 2014년 8월 15일
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 2014년 8월 15일
I will ask this from my friends. If you find the reason please share it here.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2014년 8월 15일

댓글:

2014년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by