Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Checking Values in a For/If loop

Subject: Checking Values in a For/If loop

From: Mary

Date: 25 Apr, 2013 00:10:10

Message: 1 of 3

This is the jist of what I want to set up, I'm just fuzzy on how to go about it:

th(k)= vector of values [ko k1 k2 k3...]
time(y) = time vector of values [t1 t2 t3]
the vectors have equal lengths and are going to be plotted together

for ( to cycle through all values of th(k));
if k1/ko == .1
 then stop code and show the value of k and y that was true for
else
continue cycling through each th(k) vector value until that becomes true
(example next cycle would check k2/k1 ==.1 etc)

ko = initial value
k1 = value right next to it

I havent worked with vectors in forever and I would appreciate any help!

Thanks!

Subject: Checking Values in a For/If loop

From: Josh Meyer

Date: 25 Apr, 2013 13:04:57

Message: 2 of 3

I can't tell what relation there is between your time(y) vector and the
other, so I can't really include it here.

j=1;
for i=2:length(th)
    if th(i)/th(i-1)==.1
        A(j) = i;
        j = j+1;
    end
end

for ex, with
th = [100 10 1 0.5 0.25];
this loop returns
A =

     2 3

You may wish to preallocate A as a vector of zeros if you're dealing with a
large number of checks.

"Mary " <22luongo@cardinalmail.cua.edu> wrote in message
news:kl9s92$ptr$1@newscl01ah.mathworks.com...
> This is the jist of what I want to set up, I'm just fuzzy on how to go
> about it:
>
> th(k)= vector of values [ko k1 k2 k3...]
> time(y) = time vector of values [t1 t2 t3]
> the vectors have equal lengths and are going to be plotted together
>
> for ( to cycle through all values of th(k));
> if k1/ko == .1
> then stop code and show the value of k and y that was true for
> else
> continue cycling through each th(k) vector value until that becomes true
> (example next cycle would check k2/k1 ==.1 etc)
>
> ko = initial value
> k1 = value right next to it
>
> I havent worked with vectors in forever and I would appreciate any help!
>
> Thanks!

Subject: Checking Values in a For/If loop

From: Steven_Lord

Date: 25 Apr, 2013 13:34:00

Message: 3 of 3



"Josh Meyer" <jmeyer@mathworks.com> wrote in message
news:klb9ls$ppj$1@newscl01ah.mathworks.com...
> I can't tell what relation there is between your time(y) vector and the
> other, so I can't really include it here.
>
> j=1;
> for i=2:length(th)
> if th(i)/th(i-1)==.1

This is a Bad Idea; I would compare here with a tolerance or test that
10*th(i) == th(i-1). See question 1 in the Math/Algorithms section of the
newsgroup FAQ for why exact, bit-for-bit equality testing between noninteger
valued floating-point numbers can result in answers you don't expect.

http://matlab.wikia.com/wiki/FAQ

Since you don't change th inside the loop, you can even get rid of the loop
entirely.

th = [100 10 1 0.5 0.25]
A = find(10*th(2:end) == th(1:end-1))+1

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us