1. PF Contest - Win "Conquering the Physics GRE" book! Click Here to Enter
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Help - Comparing values/Indexing

  1. Mar 18, 2010 #1
    1. The problem statement, all variables and given/known data

    I am very, very new to MATLAB, and can't seem to figure out this seemingly simple problem.

    Given a t-array and a y-array for a decaying exponential function, my task is to find the first relative maximum in the graph without using Matlab's preset relative max. functions. I'm attempting to do this by comparing y-values at a certain "t" to y-values at "t0" and "t1" (before and after "t"). I'm using indexing, for loops, etc.

    2. Relevant equations


    3. The attempt at a solution

    function[max,t_max] = relMax(t,y)
    for i = 1:.001:length(t);
    y(:,i+1) = y;
    y0 = y(:,i);
    y1 = y(:,i+2);
    if (y0<y && y>y1)
    max = y;
    t_max = t;


    Clearly I am making some kind of stupid mistake here. I don't really know how to use indexing very well, and it's causing me problems.

    Thanks in advance!
    Last edited: Mar 18, 2010
  2. jcsd
  3. Mar 19, 2010 #2


    User Avatar

    Indexes must be integers. You should make your loop as
    for i = 1: length(t)

    0.001 is the increment in t, not in the index.
  4. Mar 19, 2010 #3
    Thank you! I figured out what I was doing wrong. :)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - MATLAB Help Comparing Date
Need help with matlab matrices Nov 27, 2015
Building a truss with matlab -- Need help Nov 22, 2015
Matlab help please... Nov 14, 2015
Matlab error, can you help? Jan 30, 2015
MATLAB help Nov 11, 2014