Question

In: Computer Science

Rewrite the following Matlab code so it does the same thing but with complete new variable...

Rewrite the following Matlab code so it does the same thing but with complete new variable names and structure.


function stump = stumpGenerator(dataX, dataY, Dt)
intervals = 100;

rangex1 = max(dataX(:,1)) - min(dataX(:,1));
rangex2 = max(dataX(:,2)) - min(dataX(:,2));

width = (rangex1/intervals);
height = (rangex2/intervals);

starterx1 = min(dataX(:,1)) - (width/2);
starterx2 = min(dataX(:,2)) - (height/2);

currepsilon = inf;

stump = [0,0,0,1,0,0];

for i = 1:(intervals + 1)
  
horzRightError = sum(Dt(find(((dataX(:,1) - starterx1) .* dataY) < 0)));
horzLeftError = sum(Dt(find(((dataX(:,1) - starterx1) .* dataY) > 0)));
vertUpError = sum(Dt(find(((dataX(:,2) - starterx2) .* dataY) < 0)));
vertDownError = sum(Dt(find(((dataX(:,2) - starterx2) .* dataY) > 0)));
  
if (horzRightError <= horzLeftError)
horzError = horzRightError;
else
horzError = -horzLeftError;
end
  
if (vertUpError <= vertDownError)
vertError = vertUpError;
else
vertError = -vertDownError;
end
  
  
if (abs(horzError) <= abs(vertError))
if (currepsilon > abs(horzError))
currepsilon = abs(horzError);
stump(1) = 1;
stump(2) = 0;
stump(3) = starterx1;
stump(4) = horzError/(abs(horzError));
stump(5) = (log((1 - currepsilon)/currepsilon))/2;
stump(6) = currepsilon;
else
% do nothing, we already have the best stump
end
  
else
if (currepsilon > abs(vertError))
currepsilon = abs(vertError);
stump(1) = 0;
stump(2) = 1;
stump(3) = starterx2;
stump(4) = vertError/(abs(vertError));
stump(5) = (log((1/currepsilon) - 1))/2;
stump(6) = currepsilon;
else
% do nothing, we already have the best stump
end
end

starterx1 = starterx1 + width;
starterx2 = starterx2 + height;
end

end

Solutions

Expert Solution

function stp = stpGenerator(dtx, dty, ht)

hi = 100;

rx1 = max(dtx(:,1)) - min(dtx(:,1));

rx2 = max(dtx(:,2)) - min(dtx(:,2));

ww = (rx1/hi);

hh = (rx2/hi);

stx1 = min(dtx(:,1)) - (ww/2);

stx2 = min(dtx(:,2)) - (hh/2);

cpse = inf;

stp = [0,0,0,1,0,0];

for i = 1:(hi + 1)

hrErr = sum(ht(find(((dtx(:,1) - stx1) .* dty) < 0)));

hlErr = sum(ht(find(((dtx(:,1) - stx1) .* dty) > 0)));

vuErr = sum(ht(find(((dtx(:,2) - stx2) .* dty) < 0)));

vdErr = sum(ht(find(((dtx(:,2) - stx2) .* dty) > 0)));

if (hrErr <= hlErr)

hErr = hrErr;

else

hErr = -hlErr;

end

if (vuErr <= vdErr)

vErr = vuErr;

else

vErr = -vdErr;

end

if (abs(hErr) <= abs(vErr))

if (cpse > abs(hErr))

cpse = abs(hErr);

stp(1) = 1;

stp(2) = 0;

stp(3) = stx1;

stp(4) = hErr/(abs(hErr));

stp(5) = (log((1 - cpse)/cpse))/2;

stp(6) = cpse;

else

% do nothing, we already have the best stp

end

else

if (cpse > abs(vErr))

cpse = abs(vErr);

stp(1) = 0;

stp(2) = 1;

stp(3) = stx2;

stp(4) = vErr/(abs(vErr));

stp(5) = (log((1/cpse) - 1))/2;

stp(6) = cpse;

else

% do nothing, we already have the best stp

end

end

stx1 = stx1 + ww;

stx2 = stx2 + hh;

end

end


Related Solutions

Rewrite this code of a game of Moropinzee so that it works as intended without the...
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code. Code: import java.util.*; public class Moropinzee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:"); int p1 = sc.nextInt(); while(p1<1 || p1>5) { System.out.println("Invalid choice, Player 1. Enter a number 1-5:"); p1 = sc.nextInt(); } System.out.println("Player 2...
Write a MATLAB code to calculate the electricity usage in a residential home. Note: Thing that...
Write a MATLAB code to calculate the electricity usage in a residential home. Note: Thing that impact the charge could be number of rooms, square feet, electric appliances. Plot the electricity charge in terms of the variability.
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Can you please take this code and just rewrite it so it can be easily be...
Can you please take this code and just rewrite it so it can be easily be able to put into a complier. in java Implementation of above program in java: import java.util.HashMap; import java.util.Scanner; import java.util.Map.Entry; public class Shopping_cart {       static Scanner s= new Scanner (System.in);    //   declare hashmap in which key is dates as per mentioned and Values class as value which consists all the record of cases    static HashMap<String,Item> map= new HashMap<>();    public...
This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank...
This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank in the shape of an inverted cone. The radius of the tank at the top rim is 3 m, and the total height of the tank is 4 m. The volume of the tank in m3 is given by: V = (1/3) R2 H. The volume of liquid in the tank when filled to a height h measured from the bottom vertex is: V...
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test...
Need someone to rewrite this code to work in the same way: int testScore;    //Numeric test score         String input;     //To hold the user's input                 // Get the numeric test score.         input = JOptionPane.showInputDialog("Enter your numeric test score and I will tell you the grade: ");         testScore = Integer.parseInt(input);                 //Display the grade.         if (testScore < 60)            JOptionPane.showMessageDialog(null, "Your score of "+testScore+" is an F.");         else if (testScore < 70)            JOptionPane.showMessageDialog(null,...
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is...
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is a mixture of the 3 signals. Create a bandpass filter that only selects the center frequency. Output the filtered signal which contains only the middle frequency. Plot in time and frequency domain the original signal and the filtered signal. Show the output for filter order 1 and 15. Upload a pdf of the image files. Each figure should have your name in the title...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
(MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) Please complete the following...
(MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) (MATLAB ONLY) Please complete the following Question in MATLAB ASAP, Thanks. :) 2a. Write a function that outputs the amount of freezing point depression (in degrees C) given a mass of magnesium chloride salt and a volume of water. Formula to calculate freezing point depression: ΔT = iKm in which ΔT is the change in temperature in °C, i is the van't Hoff factor, which = 3 for MgCl2 because...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT