HW2_Questions.pdf - Homework 2 MSA 401 R Programming Professor Peter Rossi Question 1 One way to access elements of a matrix is to create an \u201cindex

HW2_Questions.pdf - Homework 2 MSA 401 R Programming...

This preview shows page 1 - 3 out of 3 pages.

Homework 2 MSA 401 - R Programming Professor Peter Rossi Question 1One way to access elements of a matrix is to create an “index matrix” with two columns, where each row ofthis index matrix gives the[i, j]index of the elements you want to access. For example:mat_orig <-matrix(1:9,ncol=3)mat_orig##[,1] [,2] [,3]## [1,]147## [2,]258## [3,]369mat_index <-matrix(c(1,1,3,2),ncol=2,byrow=TRUE)mat_index##[,1] [,2]## [1,]11## [2,]32mat_orig[mat_index]## [1] 1 61a) Write code to extract the diagonal elements (i.e., the elements where the row and column index are thesame) for a general square matrix and test it on an example. You may not use thediag()function.1b) Write code to extract the first band below the main diagonal of a general square matrix. (Note that the“bands” of a matrix are the diagonals above and below the main diagonal. The first band below the main 1
Image of page 1
Image of page 2
Image of page 3

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture