我需要帮助在同一图上以不同颜色绘制 if/else 命令的不同排列

I need help plotting different permutations of an if/else command in different colors on the same plot

基本上我有一个代码,它可以生成成本和可靠性之间所有可能排列的图表。共有 864 个数据点分为 8 行。其中五行有 2 个选项,其中三行有 3 个选项。

这里是我的代码的副本。我试图让 'Other Cameras' 和 'Depth & Structure Testing' 的排列与其他六种可能性具有不同的颜色。我尝试使用 'gscatter' 命令,但运气不佳。

我相信我需要在 if/else 语句本身中使用分散命令,尽管我不太确定在 'X' 和 'Y' 中为 'scatter' 命令。目前我的代码设置为以一种颜色绘制所有数据。我用 'gscatter' 删除了我的代码,因为我遇到了很多错误,当我试图修复它们时,情节最终没有按计划进行。

% Pareto_Eval
baseline_cost = 45;
nrows = 8;
%Initialize Variables
for aa = 1:nrows
   cost_delta(aa) = 0;
   reliability(aa) = 1;
end
icount = 1;

   %Propulsion
for row1 = 1:2  
    if row1 == 1
        cost_delta(1)= -7;
        reliability(1) = 0.995;
    elseif row1==2
        cost_delta(1)=0;
        reliability(1)=.99;
    end


    %Entry Mode
for row2 = 1:2
    if row2 == 1
        cost_delta(2) = -3;
        reliability(2) = .99;
    else
        cost_delta(2) = 0;
        reliability(2) = .98;
    end


    %Landing Method
for row3 = 1:3
    if row3 == 1                %if needs declaration
        cost_delta(3)= 0;
        reliability(3) = .99;
    elseif row3 == 2            %elseif needs declaration
        cost_delta(3) = 4;
        reliability(3) = .995;
    else                        %else does not need declaration
        cost_delta(3) = -2;
        reliability(3) = .95;
    end


    %Lander Type
for row4 = 1:3    
    if row4 == 1
        cost_delta(4)= 10;
        reliability(4) = .99;
    elseif row4 == 2
        cost_delta(4) = 0;
        reliability(4) = .99;
    else
        cost_delta(4) = 15;
        reliability(4) = .95;
    end


    %Rover Type
 for row5 = 1:2
    if row5 == 1
        cost_delta(5)= -2;
        reliability(5) = .98;
    else
        cost_delta(5) = 0;
        reliability(5) = .975;
    end


    %Power Source
for row6 = 1:2
    if row6 == 1
        cost_delta(6) = -3;
        reliability(6) = .95;
    else
        cost_delta(6) = 0;
        reliability(6) = .995;
    end   

    %Depth & Structure Testing
for row7 = 1:2
    if row7 == 1
        cost_delta(7) = 0;
        reliability(7) = .99;
    else 
        cost_delta(7) = 2;
        reliability(7) = .85;
    end      

      %Other Cameras
for row8 = 1:3    
    if row8 == 1
        cost_delta(8)= -1;
        reliability(8) = .99;
    elseif row8 == 2
        cost_delta(8) = -1;
        reliability(8) = .99;
    else
        cost_delta(8) = 0;
        reliability(8) = .9801;
    end

    cost_delta_total = 0;
    reliability_product = 1;

    for bb=1:nrows
        cost_delta_total = cost_delta_total + cost_delta(bb);
        reliability_product = reliability_product*reliability(bb);
    end

    total_cost(icount) = baseline_cost + cost_delta_total;
    total_reliability(icount) = reliability_product;
    icount = icount + 1;

end; end; end;      %Rows 1,2,3
end; end; end;      %Rows 4,5,6 
end; end;           %Rows 7,8


%Plot the Pareto Evaluation    
fignum=1;
figure(fignum)
sz = 5;
scatter(total_reliability, total_cost, sz, 'blue')
xlabel('Reliability')
ylabel('Cost')
title('Pareto Plot')   

感谢任何帮助。我没有太多使用 Matlab 的经验,我尝试四处寻找帮助,但没有任何效果。

这是我创建的一个示例代码,可以简化问题:

% Pareto_Eval
baseline_cost = 55;
nrows = 3;


%Initialize Variables
for aa = 1:nrows
   cost_delta(aa) = 0;
   reliability(aa) = 1;
end
icount = 1;

%Group 1
for row1 = 1:2
    if row1 == 1
        cost_delta(1)= 5;
        reliability(1) = 0.999;  
    elseif row1==2
        cost_delta(1) = 0;      
        reliability(1) = .995;  
    end

    %Group 2
    for row2 = 1:2         
      if row2 == 1
        cost_delta(2) = 0;    
        reliability(2) = .98;
      else              
        cost_delta(2) = -2;
        reliability(2) = .95;
      end

      %Group 3
      for row3 = 1:2
        if row3 == 1
          cost_delta(3) = 3;   
          reliability(3) = .997;
         else                  
          cost_delta(3) = 0;
          reliability(3) = .96;
        end

       %initializing each row      
       cost_delta_total = 0;
       reliability_product = 1;

        for bb = 1:nrows   
          cost_delta_total = cost_delta_total + cost_delta(bb);  
          reliability_product = reliability_product*reliability(bb); 
        end


       total_cost(icount) = baseline_cost + cost_delta_total;
       total_reliability(icount) = reliability_product;
       icount = icount + 1;
      end
    end
end

fignum=1;
figure(fignum)
sz = 25;
scatter(total_reliability, total_cost, sz)
xlabel('Reliability')
ylabel('Cost')
title('Pareto Plot')

基本上我需要在每个 if 循环中绘制一个图,但我不确定该怎么做并将它们都放在同一个图上

听起来是个有趣的项目!不确定我是否正确理解了您的预期情节,但希望下面的代码能让您更接近您正在寻找的内容。

我开始时使用了相当深的嵌套 for 循环(就像您所做的那样),但通过构建排列矩阵使其更加简洁。

counter = 0;
for propulsion_options = 1:2
    for entry_mode = 1:2
        for landing_method = 1:3
            for lander_type = 1:3
                for rover_type = 1:2
                    for power_source = 1:2
                        for depth_testing = 1:2
                            for other_cameras = 1:3
                                counter = counter +1
                                permutations(counter,:) = [...
                                    propulsion_options,...
                                    entry_mode,...
                                    landing_method,...
                                    lander_type,...
                                    rover_type,...
                                    power_source,...
                                    depth_testing,...
                                    other_cameras];
                            end
                        end
                    end
                end
            end
        end
    end
end

通过这种方式,我将实际得分排除在循环之外,并且可能更容易调整值。我将成本和可靠性数组初始化为与排列数组相同的大小:

cost_delta = zeros(size(permutations));
reliability = zeros(size(permutations));

然后对于每个指标,我在排列数组中搜索每个可能值的所有出现并分配适当的分数:

%propulsion
propertyNo = 1;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = -7;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 0;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.995;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.99;

%entry_mode (2)
propertyNo = 2;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = -3;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 0;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.98;

%landing_method (3) 
propertyNo = 3;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = 0;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 4;
cost_delta(find(permutations(:,propertyNo)==3),propertyNo) = -2;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.995;
reliability(find(permutations(:,propertyNo)==3),propertyNo) = 0.95;

%lander_type (3)
propertyNo = 4;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = 10;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 0;
cost_delta(find(permutations(:,propertyNo)==3),propertyNo) = 15;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==3),propertyNo) = 0.95;

%rover_type (2)
propertyNo = 5;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = -2;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 0;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.98;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.975;

%power_source (2)
propertyNo = 6;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = -3;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 0;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.95;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.995;

%depth_testing (2)
propertyNo = 7;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = 0;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = 2;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.85;

%other_cameras (3)
propertyNo = 8;
cost_delta(find(permutations(:,propertyNo)==1),propertyNo) = -1;
cost_delta(find(permutations(:,propertyNo)==2),propertyNo) = -1;
cost_delta(find(permutations(:,propertyNo)==3),propertyNo) = 0;
reliability(find(permutations(:,propertyNo)==1),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==2),propertyNo) = 0.99;
reliability(find(permutations(:,propertyNo)==3),propertyNo) = 0.9801;

然后每个排列都可以通过沿第二个维度对产品求和并获得总成本/可靠性分数:

cost_delta_total = sum(cost_delta,2);
reliability_product = prod(reliability,2);

最后,您可以绘制所有点(按照您的原件):

%Plot the Pareto Evaluation    
fignum=1;
figure(fignum)
sz = 5;
scatter(reliability_product, cost_delta_total, sz, 'b')
xlabel('Reliability')
ylabel('Cost')
title('Pareto Plot')   

或者您可以通过搜索特定的 属性 值并绘制这些不同的颜色来创建排列的索引(实际上,这个位回答了您最具体的问题,即如何在同一轴上绘制两个东西 - 您只需要 hold on; 命令):

propertyNo = 7;
indexDepth1 = find(permutations(:,propertyNo)==1);
indexDepth2 = find(permutations(:,propertyNo)==2);
fignum=2;
figure(fignum)
sz = 5;
scatter(reliability_product(indexDepth1), cost_delta_total(indexDepth1), sz, 'k');
hold on;
scatter(reliability_product(indexDepth2), cost_delta_total(indexDepth2), sz, 'b');
xlabel('Reliability')
ylabel('Cost')
title('Pareto Plot')   
legend('Depth & Structure Test 1','Depth & Structure Test 2')

propertyNo = 8;
indexCam1 = find(permutations(:,propertyNo)==1);
indexCam2 = find(permutations(:,propertyNo)==2);
indexCam3 = find(permutations(:,propertyNo)==3);
fignum=3;
figure(fignum)
sz = 5;
scatter(reliability_product(indexCam1), cost_delta_total(indexCam1), sz, 'k');
hold on;
scatter(reliability_product(indexCam2), cost_delta_total(indexCam2), sz, 'b');
scatter(reliability_product(indexCam3), cost_delta_total(indexCam3), sz, 'g');
xlabel('Reliability')
ylabel('Cost')
title('Pareto Plot')   
legend('Other Camera 1','Other Camera 2','Other Camera 3')

祝你任务顺利!发射日是什么时候?