首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要帮助在同一幅图上绘制不同颜色的if/ need命令的不同排列

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

Stack Overflow用户
提问于 2019-09-17 01:29:14
回答 1查看 62关注 0票数 1

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

这是我的代码副本。我试图让‘其他相机’和‘深度和结构测试’的排列有一个不同的颜色与其他六种可能性。我试着使用“gscatter”命令,但没有多少运气。

我相信我需要在if/ for语句本身中使用分散命令,尽管我不太确定在'X‘和'Y’中为“分散”命令绘制什么。当前,我的代码被设置为用一种颜色绘制所有数据。我删除了我的代码,因为我有很多错误,当我试图修复它们时,情节最终没有按计划工作。

代码语言:javascript
复制
% 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没有太多的经验,我试着四处寻找帮助,但没有什么真正的工作。

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

代码语言:javascript
复制
% 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-循环中绘制一个图,但我不知道如何做到,并将它们都放在同一块图上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-17 22:12:43

听起来是个有趣的项目!不确定我是否正确地理解了您的意图,但希望下面的代码能使您更接近您所要寻找的内容。

我首先从嵌套的for循环开始(就像您所做的那样),但是通过构建排列矩阵来保持它更简洁。

代码语言:javascript
复制
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

这样,我就可以避免循环中的实际评分,并且可能更容易调整值。我将成本和可靠性数组初始化为与排列数组相同的大小:

代码语言:javascript
复制
cost_delta = zeros(size(permutations));
reliability = zeros(size(permutations));

然后,对于每个度量,我在排列数组中搜索每个可能值的所有发生,并分配适当的分数:

代码语言:javascript
复制
%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;

然后,通过在第二个维度上对产品进行求和和计算,每个置换都可以得到一个总成本/可靠性评分:

代码语言:javascript
复制
cost_delta_total = sum(cost_delta,2);
reliability_product = prod(reliability,2);

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

代码语言:javascript
复制
%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;命令):

代码语言:javascript
复制
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')

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57966222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档