Write a script asking the user to input a 3 times 3 matrix m

Write a script asking the user to input a 3 times 3 matrix, mat. Determine if the user entered the correct size matrix. If the size is not correct, output an error message. If the size is correct, Use variables to display the following elements of the matrix: mat(2, 3), mat([1 3], 2), mat(:, 2) Create a row vector containing all the elements of mat that are less than 5. Use a variable to display the row vector.

Solution

% matlab code

% 1)
for i=1:3
    for j=1:3
        fprintf(\"Enter matrix element %d %d \",i,j);
        mat(i,j) = input(\": \");
        if isnumeric(mat(i,j)) == 0
            disp(\"Invalid Input\");
            exit
        end
    end
end
disp(\"Matrix mat: \");
disp(mat);


% 2)
disp(\"mat(2,3): \");
disp(mat(2,3));
disp(\"mat([1,3],2): \");
disp(mat([1,3],2));
disp(\"mat(:,2): \");
disp(mat(:,2));

%3)
row_vector = [];
index = 1;
for i=1:3
    for j=1:3
        if mat(i,j) < 5
            row_vector(index) = mat(i,j);
            index = index + 1;
        end
    end
end
disp(\"Row vector: \");
disp(row_vector);

%{
output:

Enter matrix element 1 1 : 5
Enter matrix element 1 2 : 6
Enter matrix element 1 3 : 1
Enter matrix element 2 1 : -1
Enter matrix element 2 2 : -3
Enter matrix element 2 3 : -4
Enter matrix element 3 1 : 6
Enter matrix element 3 2 : 7
Enter matrix element 3 3 : 8

Matrix mat:
   5   6   1
-1 -3 -4
   6   7   8

mat(2,3):
-4        
mat([1,3],2):
   6      
   7      
mat(:,2):
   6      
-3      
   7

Row vector:
   1 -1 -3 -4

%}

 Write a script asking the user to input a 3 times 3 matrix, mat. Determine if the user entered the correct size matrix. If the size is not correct, output an e
 Write a script asking the user to input a 3 times 3 matrix, mat. Determine if the user entered the correct size matrix. If the size is not correct, output an e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site