在Matlab Rev2中用矩阵内的值替换最接近Nan的值

时间:2017-10-25 12:24:59

标签: matlab for-loop matrix merge nan

我努力将一些代码应用到@crazyGamer给我的不同应用程序,让我解释一下。我会解决这个问题,但现在,这是我的数据:

%% Grid setup
    O           = -40:10:+50;
    H           = [-1500,-1000:1000:20000];
    [OAT,Hp]    = meshgrid(O,H);
    HpScale     = Hp/1000;
%% Data I have   
M = [   5227.705922 5012.706706 4814.693406 4631.729567 4462.162313 4304.572261 4157.733705 4020.582668 3892.191077 3771.745739
        5199.06906  4985.247589 4788.318986 4606.357406 4437.719024 4280.992235 4134.958046 3998.558309 3870.870035 3751.084485
        5142.176372 4930.69472  4735.921079 4555.950679 4389.157683 4234.145934 4089.709777 3954.802643 3828.511643 3710.03689
        5085.788854 4876.626245 4683.988431 4505.991531 4341.027536 4187.715597 4044.863283 3911.435499 3786.529367 3669.353772
        5029.903068 4823.038866 4632.517876 4456.476916 4293.325647 4141.698394 4000.415829 3868.454232 3744.920646 3629.032648
        4974.515591 4769.929303 4581.506261 4407.4038   4246.049094 4096.091506 3956.364694 3825.85621  3703.682932 3589.071051
        4919.623016 4717.294288 4530.950449 4358.769167 4199.194971 4050.892129 3912.707168 3783.638813 3662.813688 3549.466521
        4865.221954 4665.130572 4480.847318 4310.570012 4152.760383 4006.09747  3869.440555 3741.799435 3622.31039  3510.216613
        4811.30903  4613.434918 4431.19376  4262.803346 4106.742451 3961.704751 3826.562171 3700.335479 3582.170526 3471.318893
        4757.880887 4562.204107 4381.986683 4215.466193 4061.138308 3917.711209 3784.069346 3659.244363 3542.391597 3432.770938
        4704.934182 4511.434935 4333.223008 4168.555594 4015.945101 3874.114089 3741.959422 3618.523518 3502.971114 3394.570337
        4652.465589 4461.124212 4284.899673 4122.0686   3971.159993 3830.910656 3700.229753 3578.170385 3463.906601 3356.71469
        4600.471799 4411.268764 4237.013628 4076.00228  3926.780157 3788.098181 3658.877708 3538.182418 3425.195593 3319.201609
        4548.949518 4361.865433 4189.561841 4030.353715 3882.802783 3745.673954 3617.900666 3498.557084 3386.835638 3282.028718
        4497.895467 4312.911076 4142.541292 3985.12 3839.225071 3703.635275 3577.296019 3459.291862 3348.824295 3245.19365
        4447.306384 4264.402564 4095.948976 3940.298246 3796.044237 3661.979457 3537.061175 3420.384243 3311.159136 3208.694054
        4397.179022 4216.336784 4049.781904 3895.885575 3753.257511 3620.703828 3497.193549 3381.83173  3273.837742 3172.527585
        4347.510151 4168.710638 4004.037099 3851.879125 3710.862133 3579.805727 3457.690574 3343.631838 3236.857709 3136.691913
        4298.296555 4121.521044 3958.711601 3808.276047 3668.855361 3539.282507 3418.549691 3305.782094 3200.216643 3101.184719
        4249.535035 4074.764933 3913.802463 3765.073507 3627.234463 3499.131533 3379.768356 3268.280038 3163.912162 3066.003693
        4201.222407 4028.439253 3869.306752 3722.268684 3585.996721 3459.350183 3341.344036 3231.12322  3127.941895 3031.14654
        4153.355503 3982.540965 3825.221551 3679.858771 3545.139431 3419.935849 3303.274213 3194.309205 3092.303483 2996.610972
        4105.93117  3937.067046 3781.543955 3637.840974 3504.659902 3380.885934 3265.556378 3157.835567 3056.994578 2962.394715];

如果您应该绘制此(plot(M,HpScale,'k--');axis([2400 4400 -2 20])),则最终会得到此图表:

enter image description here

我想在M = 4300上施加一条垂直硬限制线,这是由@crazyGamer解决的:

%% Right Hard limit = Correct as answered by sight
M(M > 4300) = NaN;          %Keeping matrix M and Hp the same size + exscluding data>4300
[x, y] = find(isnan(M));    % Row and column indices of NaN in array `M`

% Offset from Nan by 1 both in the positive and negative direction to find positions of neighboring elements.
% All 4-neighbor elements around each NaN
r = [x-1 x+1 x x];
c = [y y y-1 y+1];
% Delete those values that are outside the array bounds
% (For NaNs in the edges)
outInd = r < 1 | r > size(M, 1) | c < 1 | c > size(M, 2);
r(outInd) = [];
c(outInd) = [];
% Replace all these neighbors with required value
M(sub2ind(size(M), r, c)) = 4300;

这恰好产生了正确的硬限制(plot(M,HpScale,'k--');axis([2400 4400 -2 20]);xlabel('M - Momentum');ylabel('Hp - Horsepower');):

enter image description here

现在我想要左侧硬限制,但这次不是直线而是抛物线曲线,所以我首先引入抛物线来切割M矩阵,这是我的解决方案:

%% Left Hard Limit
HardLimx    = 2100:100:4300; 
HardLimy    = 0.001*HardLimx.^2+3000;    %Cutoff line y=mx^2+c

% Split up data(From exsisting data Hp & Array)
MSplit     = M;                                      
HpSplit  = Hp;                                   

%Interpolate(<0 if point below cut function; >0 if pont above cut function)
DecisionPoint = interp1(HardLimx,HardLimy,M)-Hp;        

%Assign the values for z(where <0 if point below cut function; >0 if point
%above cut function)by eliminating(NaN) the points meeting the condition.

M(DecisionPoint<=0)             = NaN;                              
Hp(DecisionPoint<=0)            = NaN;                            
MSplit(DecisionPoint>0)         = NaN;                                  
HpSplit(DecisionPoint>0)        = NaN;

现在让我的剪切线分割我的M矩阵,用视觉图示(plot(HardLimx,HardLimy/1000,'g');axis([2400 4400 -2 20]);xlabel('M - Momentum');ylabel('Hp - Horsepower'); plot(M,Hp/1000,'k--');):

enter image description here

你可以看到M矩阵已被切割得恰到好处,但是M矩阵在它被切割的地方是开放的,并且没有像右侧硬限制一样被关闭。我希望它被关闭。所以,我尝试使用相同的代码进行正确的硬限制,稍作调整,但我没有成功,这是我的尝试:

%%%%%%%%%%%%%%%%%InternetQuestion%%%%%%%%%%%%%%%%%%%%%
CutMatrix = meshgrid(HardLimx,HardLimy);
for i=1:length(HardLimx),
    HardLimx(i);
    M(sub2ind(size(M), s, d)) = HardLimx(i);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
And now to visually see the results:

hold on
    plot(M,HpScale,'k--');                 %Below cut M matrix 
    plot(HardLimx,HardLimy/1000,'g');      %cutting line
    grid
    axis([2400 4400 -2 20])
    shg

我的解决方案导致图形看起来像这样,这是不正确的,因为我没有设法缩小差距:

enter image description here

请帮忙!

0 个答案:

没有答案