Best way to lookup values in table...

6 vues (au cours des 30 derniers jours)
Matt
Matt le 4 Avr 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!

Réponse acceptée

Matt
Matt le 4 Avr 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;

Plus de réponses (1)

Peter Perkins
Peter Perkins le 6 Avr 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)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by