Best way to lookup values in table...

6 views (last 30 days)
Matt
Matt on 4 Apr 2018
Answered: Peter Perkins on 6 Apr 2018
Hi, I have a 24x12 (hourly x monthly) table of values, and a timetable that I want to look up a specific hour/month value in the Matrix. I am using the following code that uses the already calculated Hour/Month in the timetable, but its pretty slow and I'd expect there is a better/faster method.
t = 24*12 table of data values i want to look up HourlyProfile = hourly timetable with columns for Month and Hour (only used for lookup below)
for i=1:size(HourlyProfile,1)
HourlyProfile.Data(i) = t{HourlyProfile.Hr(i)+1, HourlyProfile.Mo(i)};
end
Is there a faster way to look up these values in the t table?
Thanks in advance for your help!

Accepted Answer

Matt
Matt on 4 Apr 2018
As it turns out, the above code referencing tables took about 8 seconds to run, after replacing all the tables to look up from arrays/matrices then add the calc'd array to the table it now takes less than 1/3 of a second, much better.
For reference to anyone else,
t2=t{:,:};
Hrs = HourlyProfile.Hr;
Mos = HourlyProfile.Mo;
%tic
for i=1:size(HourlyProfile,1)
Gen(i) = t2(Hrs(i)+1, Mos(i));
end
%toc
HourlyProfile.Gen = Gen;

More Answers (1)

Peter Perkins
Peter Perkins on 6 Apr 2018
You don't need a loop to do this. Try this:
HourlyProfile.Data = t{HourlyProfile.Hr+1, HourlyProfile.Mo}
or even
HourlyProfile.Data = t.(HourlyProfile.Mo)(HourlyProfile.Hr+1)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!