簡體   English   中英

Matlab復雜的for-loop相關計算

[英]matlab complex for-loop correlation calcul

這是我擁有的腳本。 直到------分離為止。 在我沒有從Matlab得到任何錯誤,但我也沒有得到bestDx或bestDy的回報。 請幫忙。 (第一部分只是為了讓您了解環境)

    %%
% Variables after running script Read_eA3_file.m 
%date_time_UTC
%reflectivity
%clutter_mask

%Convert units
dBZ = reflectivity * 0.375 - 30;
dBZ_Mask = clutter_mask * 0.375 - 30;

%Replace clutter values with NaN
weather = NaN(size(dBZ));   %initialise to constant
weather(dBZ>=dBZ_Mask) = dBZ(dBZ>=dBZ_Mask); %copy values when A >= B

%Reduce to range -- those are 384x384 arrays
dBZ_range = dBZ(:,:,1:16);  %16:18 to 16:23 included
weather_range = weather(:,:,1:16); %16:18 to 16:23 included
weather1618 = weather(:,:,1); %16:18 map only
weather1623 = weather(:,:,16); %16:23 map only

% Plot maps
image(imrotate(-weather1618,90));  %of 16:18
image(imrotate(-weather1623,90));  %of 16:23

%Find x,y of strongest dBZ 
%Since the value are all negative. I look for their minimun
[M,I] = min(weather1618(:));  %for 16:18
[I_row, I_col] = ind2sub(size(weather1618),I); %values are 255 and 143
[M2,I2] = min(weather1623(:));  %for 16:23
[I2_row, I2_col] = ind2sub(size(weather1623),I2); %values are 223 and 7

%Calc displacement
%I get a value of 139.7140
max_displ=sqrt((I2_row-I_row)^2+(I2_col-I_col)^2); %between 1618 and 1623

%%
% -----Section below does not work; ONLY RUN the section ABOVE---------

%% Find Dx Dy for max_corr between two maps 
maxCoeff=0;
weather1618Modified = zeros(384,384);   %create weather array for time range
%weather1618Modified(:) = {NaN};      % Matlab cannot mix cell & double

%%
for x = 1:384
    for y = 1:384
        %30 pixel appx. 
        for Dx = -max_displ:30: max_displ 
            for Dy = -max_displ:30: max_displ

                %Limit range of x+Dx and y+Dy to 1:384
                if x+Dx<1 | y+Dy<1 | x+Dx>384 | y+Dy>384
                    continue

                    %weather1618Modified is the forecasted weather1823
                    weather1618Modified(x+Dx,y+Dy) = weather1618(x,y)

                    %Find the best correlation; Is corrcoef the right formula?
                    newCoeff=corrcoef(weather1623,weather1618Modified);
                    if newCoeff>maxCoeff
                        maxCoeff=newCoeff;
                        bestDx=Dx;
                        bestDy=Dy;
                    end
                end
            end
        end
    end
end


%% Calc displacement
bestDispl = sqrt(bestDx^2+bestDy^2);  %bestDispl for a 5 min frame

%Calc speed
speed = bestDispl/time;

您必須在第一個if之后刪除continue語句(或將其放置在其他位置)。

Continue語句使程序跳過for循環的其余部分,直接進入下一個迭代。 因此,將永遠不會設置bestDx和bestDy。

文檔: https : //se.mathworks.com/help/matlab/ref/continue.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM