【问题标题】:matlab complex for-loop correlation calculmatlab复数for循环相关计算
【发布时间】:2017-10-29 11:08:16
【问题描述】:

这是我的脚本。它一直工作到 ------ 分离。在我没有从 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;

【问题讨论】:

  • 来自指南:“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现所需的最短代码它在问题本身。没有明确问题陈述的问题对其他读者没有用"
  • 请使用 matlab 调试器并尝试理解为什么从未设置变量。

标签: arrays matlab loops for-loop plot


【解决方案1】:

您必须在第一个 if 之后删除 continue 语句(或将其放在其他地方)。

continue 语句使程序跳过 for 循环的剩余部分,直接进入下一次迭代。因此 bestDx 和 bestDy 永远不会被设置。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多