필터 지우기
필터 지우기

Best way to lookup values in table...

조회 수: 9 (최근 30일)
Matt
Matt 2018년 4월 4일
답변: Peter Perkins 2018년 4월 6일
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!

채택된 답변

Matt
Matt 2018년 4월 4일
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;

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 4월 6일
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)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by