Question

R programming 1. Write an assignment statement to complete each of the following without using diff()...

R programming

1. Write an assignment statement to complete each of the following without using diff() and sign(): (a) generate a vector of the difference between the ith element and the (i+1)th element in a vector v of integers, where i = 1: length(v), and (b) generate a vector of the sign of each elements in the vector generated in (a) as follows: ‘p’ for positive, ‘n’ for negative. (Use v ⟵ c(1,4,7,2,1) for testing.)

2. Write a function that assigns an integer value, say 7, to an integer n and generate a symmetric metrics where the diagonal elements are all zeros, and the elements following the 0 value are consecutive odd numbers. For example, the first row of a 5X5 matrix contains “0, 1, 3, 5, 7”; the second row contains “1, 0, 3, 5, 7”. You may use any function that we have used in our lectures to write your function. (You cannot hard-code the matrix; it needs to be generated.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

SOURCE CODE:

v=c(1,4,7,2,1)
ans=c()
for(i in 1:length(v)){
if(i+1<=length(v))
ans=c(ans,(v[i]-v[i+1]))
}
print(ans)
ans2=c()
for(i in 1:length(ans)){
if(ans[i]<0)
ans2=c(ans2,'n')
else
ans2=c(ans2,'p')
}
print(ans2)

SCREENSHOTS:

2.

SOURCE CODE:

library(R.utils)
n=7
tup=c()
i=1
while(i!=n+2)
{
tup=c(tup,i)
i=i+2
}
len=length(tup)+1
m=matrix(,nrow=0,ncol=len)
for (i in 1:len)
{
temp=insert(tup,at=i,values=0)
m=rbind(m,temp)
}
print(m)

SCREENSHOTS:

Add a comment
Know the answer?
Add Answer to:
R programming 1. Write an assignment statement to complete each of the following without using diff()...
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
  • Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...

    Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...

  • Using RStudio, Create an R-Script: (1) You will generate a numeric matrix of size 21 by...

    Using RStudio, Create an R-Script: (1) You will generate a numeric matrix of size 21 by 21 and will name it tmp. To do so, draw 21×21 = 441 random observations from the standard normal distribution. Before doing so, set the seed for the random number generator to 37. See help for set.seed(). (2) Change the diagonal elements of tmp to 1s. (3) Calculate condition number of tmp. See help for cond(). (4) Calculate the inverse of tmp. See help...

  • Using RStudio, Create an R-Script: (1) You will generate a numeric matrix of size 21 by...

    Using RStudio, Create an R-Script: (1) You will generate a numeric matrix of size 21 by 21 and will name it tmp. To do so, draw 21×21 = 441 random observations from the standard normal distribution. Before doing so, set the seed for the random number generator to 37. See help for set.seed(). (2) Change the diagonal elements of tmp to 1s. (3) Calculate condition number of tmp. See help for cond(). (4) Calculate the inverse of tmp. See help...

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • The problem demonstrates the use of the random number generator to recover previously generated random numbers Write a function called randi test that takes two scalar positive integer arguments maxi...

    The problem demonstrates the use of the random number generator to recover previously generated random numbers Write a function called randi test that takes two scalar positive integer arguments maxi and n, and retums two output arguments: a row vector of n2 elements and and n-by-n matrix. The two output arguments must contain the exact same set of random integers that fall between 1 and maxi Do this using the random number genertor, not by reshaping the data Example n,v-randi...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • 1. Write a function in matlab platform for each part as described. a. Obtains the multiplication of elements of n-sized...

    1. Write a function in matlab platform for each part as described. a. Obtains the multiplication of elements of n-sized vector, where n is a positive integer. Name of this function must be “all2”. b. Obtains the sum of elements of nxn-sized matrix, where n is a positive integer. Name of this function must be “will3”. c. Obtains the sum of prime factors of an integer. Name of this function must be “be0”. Example (1): Prime factors of 84 are...

  • This assignment assesses the material covered in Modules 6-10. Write full and complete so- lutions, using...

    This assignment assesses the material covered in Modules 6-10. Write full and complete so- lutions, using full sentences where appropriate. Explain all row operations when computing an RREF. Answer the questions asked. Questions 1-5 are worth 20 points each. The bonus question is worth 10 points (but points on this assignment are capped at 100). Recommended Deadline: April 24th Final Deadline: May 1st. 1. Compute the inverse of the matrix A = 1 3 1 4 -1 1 2 0...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

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
ADVERTISEMENT