Indexing a value from a vector

I have a for loop where I need to index the vector element when the value exceeds 120, and display the number of iterations it took to reach that value. Can anyone help me? My code:
n=100;
p(1)=14.7;
for i=2:n-1
p(i)=((patm*v1+p(i-1)*vtire)/(v2+vtire));
end
pg=p-patm
and of course all these variable are defined earlier in the script

답변 (2개)

Wayne King
Wayne King 2013년 10월 3일
편집: Wayne King 2013년 10월 3일

0 개 추천

If you don't know the number of iterations in advance, why use a for loop?
Here, I'll test when any element of p goes negative and the looking at the length of the vector, you'll know it occured in the length-1.
n = 2;
p(1) = 14.7;
while all(p>0)
p(n) = p(n-1)-0.01;
n = n+1;
end
length(p)

댓글 수: 3

Nick
Nick 2013년 10월 3일
The question asks to find how many pumps of the bike pump it takes to reach 120 psi. My loops goes simulates 101 pumps, I just need to index the pump number that makes it to 120 psi. It should be around the 24th pump
Wayne King
Wayne King 2013년 10월 3일
Then what Azzi suggests should work
Nick
Nick 2013년 10월 3일
It did'nt work, it just give me the values that are greater than 120. I just need the first value greater than 120, and the number of strokes
Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 3일
편집: Azzi Abdelmalek 2013년 10월 3일

0 개 추천

n=100;
p(1)=14.7;
for i=2:n-1
p(i)=((patm*v1+p(i-1)*vtire)/(v2+vtire));
end
pg=p-patm
idx=find(p>=120)

이 질문은 마감되었습니다.

질문:

2013년 10월 3일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by