3

I have the following sample of data

Time(s)    Speed(m/s)
1.2          7
2.5          4.2
2.6          8
3.1          12
3.6          3.2
3.9          9.1
4.3          1.6
4.6          3.8

I want to only have the data in the time range of 2-4 seconds. I can do this no problem with

Extracted_Time_Data = Data_Times(Data_Times>2 & Data_Times<4);

However I will only have the times and not the accompanying (in this case) speed value. I know I could use something like

Extracted_Speed_Data = Data(2:5,2);

which I could then use to make a matrix

End_Goal = [Extracted_Time_Data,Extracted_Speed_Data]

And I have answered my own question however what if I used another data set with hundreds of lines of data. I still want the data in the same time range 2-4 seconds but I want MATLAB to automatically store the speed with its accompanying time.

I hope the question is clear enough, thanks in advance for any help given.

Amro
  • 123,847
  • 25
  • 243
  • 454
user3536870
  • 249
  • 1
  • 2
  • 13

1 Answers1

3

Logical indexing will do it.

(Data_Times(:,1)>2 & Data_Times(:,1)<4 will select the rows. Then you just select all columns with the : operator.

rows = Data_Times(:,1)>2 & Data_Times(:,1)<4;
Data_Times(rows,:);

or

Data_Times((Data_Times(:,1)>2 & Data_Times(:,1)<4),:)

ans =

2.5000    4.2000
2.6000    8.0000
3.1000   12.0000
3.6000    3.2000
3.9000    9.1000
kkuilla
  • 2,226
  • 3
  • 34
  • 37