The loop is continuously running
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
MA
am 17 Okt. 2021
Kommentiert: Scott MacKenzie
am 17 Okt. 2021
I am trying to compare the date values for two tables named T and S, if any matching of the dates is found we should copy the value of the variable 'solar' from S and add it to its corresponding date in T. I implemented that using the following for loop, but the loop keep continuously running for some reason. and we are not getting our desired result.
having that T is 37028x5 table and S is 25550x2 table.
how can we fix that or would there be a better way to do this rather than a for loop?
for jj=1:size(T)
for ii=1:size(S)
if (year(T.Date(jj))==year(S.date(ii)) & month(T.Date(jj))==month(S.date(ii)) & day(T.Date(jj))==day(S.date(ii)) )
T.solarT(jj)=S.solar(ii);
end
end
end
attached are parts of the tables S and T respectively,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/769686/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/769691/image.png)
thanks for giving it a look,
0 Kommentare
Akzeptierte Antwort
Scott MacKenzie
am 17 Okt. 2021
Your loop is fine, but is inefficient and takes a long time to execute. Instead of the loop, try this:
[Lia, Locb] = ismember(T.Date, S.date);
idx = Locb(Locb > 0);
T.solarT(Lia) = S.solar(idx);
2 Kommentare
Scott MacKenzie
am 17 Okt. 2021
You're welcome. Glad to help.
Another approach to consider is converting the tables (which include datetime columns) to timetables and then using the synchronize function. Check the timetable documentation for examples.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time 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!