Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB vector value location ID

  1. Mar 31, 2010 #1
    HELP! MATLAB vector value location ID?

    In MATLAB, say I have two vectors of the same length -- because one is dependent on the other. In my case the independent vector is t, time, and the dependent is p, pressure.

    t = 0:500 with an arbitrary time step (finer than 0.2 seconds).

    I would like to know the easiest way to extract the value of p at t = 300.2. Any ideas?
    Last edited: Mar 31, 2010
  2. jcsd
  3. Mar 31, 2010 #2
    Re: HELP! MATLAB vector value location ID?

    Well, the problem can be restated as follows: you know the value of p at 501 evenly spaced points in time and you want to use this to infer a value for p at some intermediate point. You solve problems like this using an interpolation of some type. Matlab has a range of commands with which you can generate interpolations; since your problem is one-dimensional you'll probably want to use the interp1 command. Type the following and see where Matlab leads you.

    Code (Text):
    doc interp1
  4. Apr 1, 2010 #3
    I understand your response, shoehorn, though it is not exactly what I am looking for. Basically I'm looking for the simplest way to arrive at the result of:

    p = 1.1:0.1:10;
    t = 16:1.5:150;

    for i = 1:length(t)
    if t(i) == 53.5
    ival = i;

  5. Apr 2, 2010 #4
    p = 1.1:0.1:10;
    t = 16:1.5:150;
  6. Apr 2, 2010 #5
    AWESOME! Thanks.
  7. Jul 14, 2010 #6
    What do you mean by corresponds to? Since 5 is the 5th element of t, putting p(t==5, 2) gives 9 since that is the 5th element of the 2nd column. What would you like the output to be?
  8. Jul 14, 2010 #7
    Yea, I figured it out (which is why I removed my post). The example I had given above was different than what I was running on my machine. I discovered my "t" vector, for the simulation I was running on my computer, had machine epsilon in it. If I did p(t>5-eps & t<5+eps,2) it worked. Sorry for the confusion.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook