I have two stuctures data showing as two pictures,and I want to find the data that meets the conditions to match. Using the loop is very slow. Please give me some better advice.Thanks.
I want to match multi-filed's data in two structure data.
3 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191596/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191597/image.png)
I have two stuctures data showing as two pictures,and I want to find the data that meets the conditions to match. Using the loop is very slow. Please give me some better advice.Thanks.
if true
for i= 1:length(ap.date)
dnb = datevec(ap.date(i));
lonb=ap.lon(i);
latb=ap.lat(i);
for j=1:length(ad.date)
dna = datevec(ad.date(j));
lona=ad.lon(j);
lata=ad.lat(j);
difftime=fix(etime(dna,dnb)/3600);
difflon=lonb-lona;
difflat=latb-lata;
if (difftime>=-0.05 & difftime <=0.05 & difflon>=-0.001 & difflon <=0.001 & difflat>=-0.001 & difflat <=0.001)==1
temp=[i j]; w=[w;temp];
re_data.date(i,1)=ap.date(i);
re_data.lon(i,1)=ap.lon(i);
re_data.lat(i,1)=ap.lat(i);
re_data.date_ad(i,1)=ad.date(j);
re_data.lon_ad(i,1)=ad.lon(j);
re_data.lat_ad(i,1)=ad.lat(j);
re_data.cruise_ad(i,1)=ad.cruise(j);
re_data.depth_ad(i,1)=ad.depth(j);
elseif difftime>=-0.05 & difftime <=0.05 & difflon>=-0.001 & difflon <=0.001 & difflat>=-0.001 & difflat <=0.001
re_data.date(i,1)=ap.date(i);
re_data.lon(i,1)=ap.lon(i);
re_data.lat(i,1)=ap.lat(i);
re_data.date_ad(i,1)={-999};
re_data.lon_ad(i,1)=-999;
re_data.lat_ad(i,1)=-999;
re_data.cruise_ad(i,1)={-999};
re_data.depth_ad(i,1)=-999;
end
end
end
3 个评论
Guillaume
2018-7-1
What format is the time in the cell arrays, datestr?
What should happen if there is more than one match in ad for a single ap? Or is it guaranteed that it never happens?
Any reason you're using old matlab time functions instead of modern datetime? Which version are you using?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!