Infinite while nested in a for loop in matlab

℡╲_俬逩灬. 提交于 2019-12-02 13:19:13

I finally found the answer. I post it here for future use.

for ii = 1:length(MM(:,4))
    t = MM(ii,4);
    ind1 = ii;
    length(ind1);
    lat1 = lat(ind1);
    lon1 = lon(ind1);
    jj = ii + 1;

    while (MM(jj,4) - t <= 5)

            ind2 = jj;
            length(ind2);
            lat2 = lat(ind2);
            lon2 = lon(ind2);
            dis = distance(lat1, lon1, lat2, lon2);

            if dis <= 1
                contact = [MM(ind1,1), MM(ind2,1), t, MM(jj,4)]   

            else
                fprintf('There is no distance smaller than 1km\n')
            end

            jj = jj + 1;
    end

end

As it seems the nested for loop in the while was wrong and without any use at all. The second problem was the condition in the while. The comparison, I previously made in the while condition was written backwards and had no meaning. And the third problem was the if statement. By putting the if statement in the while loop, I had the result in the contact.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!