How to reference or filter range of data in table using information from another table?

3 次查看(过去 30 天)
I have two data tables produced from previous code. I need to create a loop which takes a data value given in one table, looks it up in another table and spits out a range of data.
One data set is dates with corresponding inflation points from the CPI. The other data table contains pairs of dates that represent an inflation episode. I would like to use the second date set (inflationepisodes) to filter the first table (cpi points) for data of just one episode for the entire time series.
So for example, in the picture below: the code would reference inlfationepisodes, and find all the data for the first inflation episode in a seperate table (ie, all data in cpi from 1968:02 to 1971:10), preferably this would be in a loop and spit out the data needed for each inflation episode.
I know these inflationepisodes are not accurate this is just for a school project I just need to figure out the looping process.

采纳的回答

Esen Ozbay
Esen Ozbay 2021-8-6
Here is my attempt:
for episodeIdx = 1:size(inflationepisodes,1)
epStart = inflationepisodes(episodeIdx, 1);
epEnd = inflationepisodes(episodeIdx, 2);
[startIdx, ~] = find(strcmp(cpit, epStart));
[endIdx, ~] = find(strcmp(cpit, epEnd));
fprintf('%6.4f\n', cpit(startIdx:endIdx, 2))
end
Of course, you can replace fprintf with a variable assignment if you need to store the data.
This code assumes that the first column of the inflationepisodes variable contains a date earlier than the second column, and that the cpit variable has months in an increasing order.
Hope this helps!
  3 个评论
Esen Ozbay
Esen Ozbay 2021-8-6
I wrote the code to print the result on the screen since I didn't know how you needed to access it. :)
Once you have cpit(startIdx:endIdx, 2) you can do whatever you want with it.
Gina Melillo
Gina Melillo 2021-8-6
Thank you for the help.
If I wanted to do something, like look at the max value of the data called, how would I do that?
Maxdata = max("WHAT DO I PUT HERE?")
Thank you again :)

请先登录,再进行评论。

更多回答(1 个)

Esen Ozbay
Esen Ozbay 2021-8-6
DesiredData = cpit(startIdx:endIdx, 2); maxData = max(DesiredData);
Hope this works! :)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by