Question

The Rescale01 function below maps/scales all items in a vector to a range from 0 to...

The Rescale01 function below maps/scales all items in a vector to a range from 0 to 1. (Also discussed in lecture slides).

Rescale01 <- function(x){

rng <- range(x,na.rm = TRUE)

(x-rng[1])/(rng[2] - rng[1])

}

The function doesn’t work when x contains +ve or –ve infinity (indicated in R as Inf and –Inf, respectively). For example, if x is

x <- c(-1,-5,3,5,-3,0,2,Inf,-Inf)

Rescale01(x) returns a vector containing

NaN NaN NaN NaN NaN NaN NaN NaN NaN

Rewrite the function Rescale01 so that -Inf is mapped to 0, and Inf is mapped to 1.

Hint:

  • to check item in a vector is Inf, use is.infinite(x) & (x>0)
  • to check item in a vector is -Inf, use is.infinite(x) & (x<0)
  • Within the function, first thing you need to do is replace all Inf by the maximum value in the vector (considering only the valid items), and replace all –Inf by the minimum value in the vector (considering only the valid items)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rescale01 <- function(x){
x[is.infinite(x) & (x>0)]=max(x[!is.infinite(x)])
x[is.infinite(x) & (x<0)]=min(x[!is.infinite(x)])
rng <- range(x,na.rm = TRUE)
(x-rng[1])/(rng[2] - rng[1])
}

: Rescale01 <- function(x){ x[is.infinite(x) & (x>0)]=max(x[!is.infinite(x)]) x[is.infinite(x) & (x<0)]=min(x[!is.infinite(x)

Add a comment
Know the answer?
Add Answer to:
The Rescale01 function below maps/scales all items in a vector to a range from 0 to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • B) (10 pts) Let D(0, oo)) be the vector space of all bounded continuous functions from [0, oo) su...

    b) (10 pts) Let D(0, oo)) be the vector space of all bounded continuous functions from [0, oo) such that R If(x) dz 00. Give an example of a sequence {fn} of functions in D(0,00)) which (i) converges pointwise for E [0, oo) to the constant function f(z)0 (ii) does not converge to 0, neither with respect to the norm, nor the Hint: it may be helpful to contemplate the phrase "mass escaping to infinity". norm. b) (10 pts) Let...

  • , A is a linear transformation that maps vectors x in 975 into vectors Let A= 0 -2 1 b in R2 Consider the set of all possible vectors b-Ax, where x is of unit length. What is the longest vector b...

    , A is a linear transformation that maps vectors x in 975 into vectors Let A= 0 -2 1 b in R2 Consider the set of all possible vectors b-Ax, where x is of unit length. What is the longest vector b in this set, and what unit length vector x is used to obtain it? You can use Matlab to save time with the computations, but please justify your answer. , A is a linear transformation that maps vectors...

  • All to be done in C++ no user input is required. Just need to create vectors...

    All to be done in C++ no user input is required. Just need to create vectors and create functions to manipulate them as described. Search 2D Vector - Part 2 Write a C++ program that uses a function to search a 2D vector of floats. The function will have 2 parameters: 1. A 2 dimensional vector of floats, v 2. The floating point number that you are searching for, item Rewrite the Search_2D_Vector function so that it can work with...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - dis...

    Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - displays the items in the cart ---------------------------------------------------------------------""" import os os.system('clear') print("\n\nCart Contents:") print("Your selected items:", cart) def catMenu(): #function that displays the category menu """catMenu function - displays the categories user picks from ---------------------------------------------------------------------""" import os os.system('clear') print(""" 1 - Books 2 - Electronics 3 - Clothing d - display cart contents x -...

  • The graph of a rational function f is shown below. Assume that all asymptotes and intercepts...

    The graph of a rational function f is shown below. Assume that all asymptotes and intercepts are shown and that the graph has no "holes", Use the graph to complete the following. (a) Write the equations for all vertical and horizontal asymptotes. Enter the equations using the "and" button as necessary. Select "None" as necessary. : None O=o (0,0) Dando Vertical asymptote(s): 1 Horizontal asymptote(s): U [0,0] (0,0) (0,0) O ovo 00 - - -8 EEE-- - -6 1 (b)...

  • Problem 3. Let D be the vector space of all differentiable function R wth the usual pointwise add...

    Problem 3. Let D be the vector space of all differentiable function R wth the usual pointwise addition and scalar multiplication of functions. In other words, for f, g E D and λ E R the function R defined by: (f +Ag) ()-f(r) +Ag(x) Let R be four functions defined by: s(x)-: sin 11 c(r) : cosz, co(z)--cos(z + θ), and so(r) sin(z + θ), and Wspanls, c Which of the following statements are true: (a) For each fixed θ...

  • The graph of a rational function fis shown below. Assume that all asymptotes and intercepts are...

    The graph of a rational function fis shown below. Assume that all asymptotes and intercepts are shown and that the graph has no "holes". Use the graph to complete the following. (a) Find all x-intercepts and y-intercepts. Check all that apply. X-intercept(s): 4 00 01 None . : O=D y-intercept(s): 01 04 00 None Dando None (0,0) HHH [0,0] (0,0] [0,0) (b) Write the equations for all vertical and horizontal asymptotes. Enter the equations using the "and" button as necessary....

  • 1. What does the below C++ statement do? vector<double> myVec(20); Group of answer choices A.It creates...

    1. What does the below C++ statement do? vector<double> myVec(20); Group of answer choices A.It creates a vector object that can only store values of 10 or less. B.It creates a vector object with a starting size of 10. C.It creates a vector object and initializes the first element with the value 20. D.It creates a vector object with a starting size of 20. 2. A recursive function contains a call to itself, but is limited to 10 recursive calls...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT