I have a vector and I want to make a while condition for its error.

I want to make abs(z(i+1)-z(i))/abs(z(i))<0.0001 in every iteration, but I don't know how to make the iteration run all the numbers in the vector z!
Can you please help me?

 Accepted Answer

Without a loop:
result = abs(diff(z) ./ abs(z(1:end-1))) < 0.0001
[EDITED, typos fixed]

More Answers (3)

z=rand(10,1);
for i=1:length(z)-1
if abs(z(i+1)-z(i))/abs(z(i))<0.0001
res(k)=1 %for example
else
res(k)=0
end
end
Despite all the help, I have a further issue.
The output of abs(diff(z)) comes in, for example, 200x1 double
The output of abs(z(1:end-1)) comes in 200x1 double too.
How can I divide (1,1) by (1,1), (2,1) by (2,1) and etc..?
If i do (top part) / (bottom part) it comes 200x200 double with a lot of zeros.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 2 Oct 2012

Community Treasure Hunt

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

Start Hunting!