Problem 1.

Will give the single random interger between 1 and 10.

n_dims <- sample(3:10,1)
print(n_dims)
## [1] 10

Based on that, write a general function to get a vector of consecutive integers from 1 to n_dims2.

my_vec <- seq(from=1, to=n_dims^2)
print(my_vec)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
# Since it was random when n_dims result was 3 the my_vec result was:
[1] 1 2 3 4 5 6 7 8 9
#When n_dims result was 9 the my_vec result was:
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
[44] 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

Use the sample function to randomly reshuffle these values.

shuffled_vector <- sample(my_vec) 
print(shuffled_vector) 
##   [1]  95  81  65  57  31  34  92  12  97  29  78  72  15  17 100  64  38  32
##  [19]  47   4  75  80  39  89  59  16  11  45  62  90  41  99  24  20  49  76
##  [37]  87  56  35  21   9  30  51  74  44  13  96  10  68  19  69  37  50   7
##  [55]  84  42  46   3   5  27  48   6  86  33  82  40  28  73  54  52  85  83
##  [73]  43  63  67  53  22  26  94  25  61  23  88  55   2  18  60  93  14   8
##  [91]  58  36  70  79  91  66  98  77  71   1

Create a square matrix with these elements.Print out the matrix.

m <- matrix(data=my_vec,nrow=n_dims)
print(m)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1   11   21   31   41   51   61   71   81    91
##  [2,]    2   12   22   32   42   52   62   72   82    92
##  [3,]    3   13   23   33   43   53   63   73   83    93
##  [4,]    4   14   24   34   44   54   64   74   84    94
##  [5,]    5   15   25   35   45   55   65   75   85    95
##  [6,]    6   16   26   36   46   56   66   76   86    96
##  [7,]    7   17   27   37   47   57   67   77   87    97
##  [8,]    8   18   28   38   48   58   68   78   88    98
##  [9,]    9   19   29   39   49   59   69   79   89    99
## [10,]   10   20   30   40   50   60   70   80   90   100

Find a function in r to transpose the matrix.print it out again and note how it has changed.

my_matrix=matrix(data=my_vec,nrow=n_dims)
transposed_matrix <- t(my_matrix)
print(transposed_matrix)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1    2    3    4    5    6    7    8    9    10
##  [2,]   11   12   13   14   15   16   17   18   19    20
##  [3,]   21   22   23   24   25   26   27   28   29    30
##  [4,]   31   32   33   34   35   36   37   38   39    40
##  [5,]   41   42   43   44   45   46   47   48   49    50
##  [6,]   51   52   53   54   55   56   57   58   59    60
##  [7,]   61   62   63   64   65   66   67   68   69    70
##  [8,]   71   72   73   74   75   76   77   78   79    80
##  [9,]   81   82   83   84   85   86   87   88   89    90
## [10,]   91   92   93   94   95   96   97   98   99   100

Calculate the sum and the mean of the elements in the first row and then the last row. Assuming that the desired result is of the transposed matrix:

first_row_sum <- sum(transposed_matrix[1,])
print(first_row_sum)
## [1] 55
first_row_mean <- mean(transposed_matrix[1,]) 
print(first_row_mean)
## [1] 5.5
last_row_sum <- sum(transposed_matrix[n_dims,])
print (last_row_sum)
## [1] 955
last_row_mean <- mean(transposed_matrix[n_dims,])
print(last_row_mean)
## [1] 95.5

Read about the eigen() function and use it on your matrix.

Look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?

ev <- eigen(transposed_matrix)
(values <- ev$values)
##  [1]  5.208398e+02+0.000000e+00i -1.583980e+01+0.000000e+00i
##  [3] -1.142138e-14+1.633587e-14i -1.142138e-14-1.633587e-14i
##  [5]  1.237539e-14+0.000000e+00i  8.193133e-15+0.000000e+00i
##  [7] -3.683161e-15+3.398555e-15i -3.683161e-15-3.398555e-15i
##  [9]  7.924536e-16+2.788653e-16i  7.924536e-16-2.788653e-16i
(vectors <- ev$vectors)
##                 [,1]           [,2]                    [,3]
##  [1,] -0.03766295+0i  0.51209464+0i  0.05352384+0.07987574i
##  [2,] -0.09082932+0i  0.40216530+0i  0.09320062-0.16934269i
##  [3,] -0.14399568+0i  0.29223595+0i -0.12498384-0.08840556i
##  [4,] -0.19716205+0i  0.18230661+0i -0.21802373+0.07598555i
##  [5,] -0.25032842+0i  0.07237726+0i -0.38387961+0.19296892i
##  [6,] -0.30349478+0i -0.03755208+0i  0.47619901+0.00000000i
##  [7,] -0.35666115+0i -0.14748143+0i  0.22631526-0.10430295i
##  [8,] -0.40982752+0i -0.25741077+0i -0.05266643+0.12372409i
##  [9,] -0.46299388+0i -0.36734011+0i  0.39669873-0.10059843i
## [10,] -0.51616025+0i -0.47726946+0i -0.46638387-0.00990467i
##                          [,4]           [,5]           [,6]
##  [1,]  0.05352384-0.07987574i  0.19881647+0i -0.16430906+0i
##  [2,]  0.09320062+0.16934269i -0.12063181+0i  0.10080298+0i
##  [3,] -0.12498384+0.08840556i  0.10132815+0i -0.01242521+0i
##  [4,] -0.21802373-0.07598555i  0.02478184+0i  0.06442974+0i
##  [5,] -0.38387961-0.19296892i -0.63697715+0i  0.62698213+0i
##  [6,]  0.47619901+0.00000000i  0.14001112+0i -0.43471309+0i
##  [7,]  0.22631526+0.10430295i  0.36445373+0i -0.52319029+0i
##  [8,] -0.05266643-0.12372409i -0.31244501+0i  0.30058647+0i
##  [9,]  0.39669873+0.10059843i  0.47408815+0i -0.05490464+0i
## [10,] -0.46638387+0.00990467i -0.23342550+0i  0.09674097+0i
##                           [,7]                     [,8]
##  [1,] -0.006003751+0.21808209i -0.006003751-0.21808209i
##  [2,]  0.017176328+0.02240989i  0.017176328-0.02240989i
##  [3,]  0.033024268-0.29586587i  0.033024268+0.29586587i
##  [4,] -0.101163246+0.14682597i -0.101163246-0.14682597i
##  [5,] -0.246255165-0.09364257i -0.246255165+0.09364257i
##  [6,]  0.551450042+0.00000000i  0.551450042+0.00000000i
##  [7,] -0.103800652-0.35803521i -0.103800652+0.35803521i
##  [8,] -0.343279174+0.28942401i -0.343279174-0.28942401i
##  [9,]  0.315868699+0.01155781i  0.315868699-0.01155781i
## [10,] -0.117017348+0.05924389i -0.117017348-0.05924389i
##                           [,9]                    [,10]
##  [1,] -0.34110231+0.068970737i -0.34110231-0.068970737i
##  [2,] -0.06431292+0.003787573i -0.06431292-0.003787573i
##  [3,]  0.30452490-0.028057264i  0.30452490+0.028057264i
##  [4,]  0.24875378-0.061912944i  0.24875378+0.061912944i
##  [5,] -0.54105578+0.000000000i -0.54105578+0.000000000i
##  [6,]  0.50995276+0.017737737i  0.50995276-0.017737737i
##  [7,]  0.29281015-0.061110609i  0.29281015+0.061110609i
##  [8,] -0.07574822-0.009209291i -0.07574822+0.009209291i
##  [9,] -0.10123893+0.047640764i -0.10123893-0.047640764i
## [10,] -0.23258342+0.022153299i -0.23258342-0.022153299i

Dig in with the typeof() function to figure out their type.

typeof(vectors)
## [1] "complex"
typeof(values)
## [1] "complex"

Checked and was able to re-run it and create a matrix of different size because n_dims will change.

Problem 2.

#Create a list with the following named elements: #my_matrix, which is a 4 x 4 matrix filled with random uniform values

my_matrix=matrix(runif(16),nrow=4,ncol=4)
print(my_matrix)
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.6402945 0.3505776 0.8367381 0.2158682
## [2,] 0.1113842 0.5402547 0.7845211 0.2878821
## [3,] 0.7474812 0.6051653 0.1818287 0.4376500
## [4,] 0.3255314 0.6436431 0.3433260 0.3515986