Lab1 Ex4

You cannot submit for this problem because the homework's deadline is due.

Ex4. Arrays and Matrices (29 marks)

(Ex4_4 is tested separately since its mark number is significantly higher than Ex4_1 through Ex4_3. The file name for this problem should be lab4.m.)

Ex4_1. Changing elements of a matrix (4 marks)

Get a matrix from user input, change its 4th through 6th elements to 0, 1 and 2 respectively, and print the modified matrix. If the matrix has less than 6 elements, print Invalid input.

Hint: Search the documentation for size() and numel().

Note: An array is also considered a matrix with only one row.

Notice what has been changed! Think about what the 4th element of a matrix is.

Sample Test Case

Input:

[100:115]

Output:

  Columns 1 through 13

   100   101   102     0     1     2   106   107   108   109   110   111   112

  Columns 14 through 16

   113   114   115

Input:

[1 2 3 4; 5 6 7 8]

Output:

     1     2     1     4
     5     0     2     8

Ex4_2. Taking a part of a matrix (5 marks)

Print as a matrix the intersection of row 2 to 5 and column 3 to 6 of a given matrix obtained from user input.

Sample Test Case

Input:

magic(7)

Output:

     7     9    18    27
     8    17    26    35
    16    25    34    36
    24    33    42    44

Ex4_3. Matrix reshaping (5 marks)

Get an integer n from user input and output a square matrix with 1, 2, ..., n on the first row, n+1, n+2, ..., 2n on the second, and so on.

Hint 1: Use reshape().

Hint 2: What is the transpose of a matrix and how is it done in MATLAB? Notice the result of reshape()'s orientation.

Sample Test Case

Input:

6

Output:

     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

Lab1 Exercises

Not Claimed
Status
Finished
Problems
5
Open Since
2022-05-19 18:15
DDL
2022-05-21 00:00
Extension
24.0 hour(s)