matlabif-statementscatter-plotpareto-chart

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


Basically I have a code where it produces a plot of all possible permutations between Cost and Reliability. There's a total of 864 data points split up between 8 rows. Five of the rows have 2 options and three of them 3 options.

Given here is a copy of my code. I'm trying to have the permutations of 'Other Cameras' and 'Depth & Structure Testing' have a different color with the other six possibilities. I tried using the 'gscatter' command but didn't have much luck with it.

I believe I need to have the scatter command in the if/else statements themselves, although I'm not too sure what to plot in the 'X' and 'Y' for the 'scatter' command. Currently my code is set up for plotting all the data in one color. I deleted my code with the 'gscatter' because I got many errors and when I tried to fix them the plot ultimately didn't work as planned.

% 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')   

Any help is appreciated. I don't have a lot of experience with Matlab and I've tried looking around for help but nothing really worked.

Here is a sample code to make questions easier I created:

% 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')

Basically I need to make a plot in each if-loop, but I'm not sure how to do it and have them all on the same plot


Solution

  • sounds like an interesting project! Not sure if I understood your intended plots correctly, but hopefully the code below gets you a bit closer to what you are looking for.

    I've started off with a rather deep mess of nested for loops (as you did) but kept it more concise bybuilding a permutations matrix.

    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
    

    This way I kept the actual scoring out of the loops, and perhaps easier to tweak the values. I initialised the cost and reliabiltiy arrays to be the same size as the permutations array:

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

    Then for each metric, I searched the permutations array for all occurances of each possible value and assigned the appropriate score:

    %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;
    

    Then each permutation can have a total cost / reliabiltiy score by summing and takign the product along the second dimension:

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

    Finally, you can plot all points (as per your original):

    %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')   
    

    or you can create an index into the permutations by searching for specific property values and plot these different colours (actually this bit answers your most specific question of how to plot two things on the same axes - you just need the hold on; command):

    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')
    

    Good luck with the mission! When is launch day?