MATLAB vector value location ID

  • Context: MATLAB 
  • Thread starter Thread starter DoubleHubble
  • Start date Start date
  • Tags Tags
    Matlab Value Vector
Click For Summary

Discussion Overview

The discussion revolves around extracting values from MATLAB vectors, specifically how to find the value of a dependent vector at a specific point in an independent vector. The context includes interpolation methods and handling precision issues in MATLAB.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks a method to extract the value of pressure (p) at a specific time (t = 300.2) from two vectors in MATLAB.
  • Another participant suggests using interpolation, specifically the interp1 command, to infer values at intermediate points.
  • A different participant provides a simple loop-based approach to find the index of a specific time value in the vector.
  • Another participant offers a more concise method using logical indexing to directly access the pressure value corresponding to a specific time.
  • One participant expresses confusion about the output related to indexing and clarifies their understanding after realizing their vector contained machine epsilon, which affected their results.
  • Areas of Agreement / Disagreement

    Participants present multiple approaches to the problem, including interpolation and indexing methods. There is no consensus on a single best method, as different participants prefer different techniques based on their understanding and the specifics of their data.

    Contextual Notes

    Participants mention issues related to machine precision and how it affects indexing in MATLAB, indicating that the solution may depend on the specific characteristics of the data being used.

DoubleHubble
Messages
8
Reaction score
0
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:
Physics news on Phys.org


DoubleHubble said:
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?

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:
doc interp1
 
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;
break
end
end

t(ival)
p(ival)
 
p = 1.1:0.1:10;
t = 16:1.5:150;
p(t==53.5)
 
AWESOME! Thanks.
 
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?
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 16 ·
Replies
16
Views
15K
  • · Replies 8 ·
Replies
8
Views
3K