Create matrices and vectors in MATLAB. Write a script to ask the user for the nu
ID: 3602926 • Letter: C
Question
Create matrices and vectors in MATLAB.
Write a script to ask the user for the number of rows and the number of columns (positive non zero integers). Your program must then generate a matrix with the size specified by the user. Let's call it mat. The elements of mat must be random integer numbers between -10 and 60. Your script then must do the following and display the result with appropriate messages 1) Display the number of rows and columns and the total number of elements in the matrix 2) Display the maximum and the minimum values in the matrix. In the next sage 3) Add another row of random numbers between 60 and 100 to mat. Your script must display the new row and the new matrix with appropriate messages. Let's call this 4) Write a command to extract the first and the last rows of matrix mat2, and display IMPORTANT: Your program must display informative messages for both input inquiry new matrix mat2. them with an appropriate message. and output display. Vague messages and bad display of results will result in losing points.Explanation / Answer
Below is the matlab code:
%Prompt user for number of rows & column in matrix
rows = input("Enter number of rows: ")
cols = input("Enter number of columns: ")
%Display matrix
display "The matrix created with random numbers is: "
mat = randi ([-10 60],rows,cols)
% (Part 1) Display rows/columns & total number of elements
display "Number of rows: " , rows
display "Number of columns: " , cols
display "Number of elements: " , rows*cols
%(Part 2) Display max and min values in matrix
display "Maximum value in the matrix: "
max(max(mat))
display "Minimum value in the matrix: "
min(min(mat))
%(Part 3) Add new row with random elements & display matrix
display "The row created with random numbers is: "
X = randi ([60 100],1,cols)
display "The new matrix created with added row is: "
mat2=[mat;X]
%(Part 4) Display the first & last row of the matrix
display "First row of the matrix:"
mat2(1,:) %Display first row
display "Last row of the matrix:"
mat2(end,:)
%End.
Output:
Pleae upvote if you like.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.