create a 3x3 matrix calculator in matlab in matlab

To create a 3x3 matrix calculator in MATLAB, you can use the built-in functions and operations available in MATLAB. Below is an example of a MATLAB script that allows you to perform various operations on a 3x3 matrix:

main.m
% Prompt the user to enter values for a 3x3 matrix
disp("Enter values for a 3x3 matrix:")
matrix = zeros(3, 3);
for i = 1:3
    for j = 1:3
        prompt = sprintf("Enter the value for element (%d, %d): ", i, j);
        matrix(i, j) = input(prompt);
    end
end

% Display the matrix
disp("Entered Matrix:")
disp(matrix)

% Menu prompt for different matrix operations
while true
    disp("Matrix Operations:")
    disp("1. Transpose")
    disp("2. Determinant")
    disp("3. Inverse")
    disp("4. Exit")
    choice = input("Enter your choice: ");
    
    % Perform the chosen operation based on user input
    switch choice
        case 1
            disp("Transpose:")
            disp(matrix');
        case 2
            disp("Determinant:")
            disp(det(matrix));
        case 3
            disp("Inverse:")
            if det(matrix) == 0
                disp("Matrix is singular and cannot be inverted.")
            else
                disp(inv(matrix));
            end
        case 4
            disp("Exiting...")
            break;
        otherwise
            disp("Invalid choice. Please try again.");
    end
end
1141 chars
46 lines

This script prompts the user to enter the values for each element of a 3x3 matrix. Then, it provides a menu with options to calculate the transpose, determinant, and inverse of the entered matrix. The script continues to prompt the user for operations until they choose to exit.

related categories

gistlibby LogSnag