Filter löschen
Filter löschen

Best way to lookup values in table...

9 Ansichten (letzte 30 Tage)
Matt
Matt am 4 Apr. 2018
Beantwortet: Peter Perkins am 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!

Akzeptierte Antwort

Matt
Matt am 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;

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 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)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by