create a simple program that can multiply two matrices using if statement in matlab. let the user input the elements in matrices. in matlab

To create a simple program in MATLAB that can multiply two matrices using if statement and user input, you can follow the steps below:

  1. Prompt the user to enter the dimensions of the matrices and create empty matrices to store the user input:
main.m
rows1 = input('Enter the number of rows for matrix 1: ');
cols1 = input('Enter the number of columns for matrix 1: ');
matrix1 = zeros(rows1, cols1);

rows2 = input('Enter the number of rows for matrix 2: ');
cols2 = input('Enter the number of columns for matrix 2: ');
matrix2 = zeros(rows2, cols2);
301 chars
8 lines
  1. Ask the user to input the elements of the first matrix:
main.m
disp('Enter elements of matrix 1: ');
for i = 1:rows1
    for j = 1:cols1
        matrix1(i, j) = input(sprintf('Enter element at position (%d, %d): ', i, j));
    end
end
172 chars
7 lines
  1. Ask the user to input the elements of the second matrix:
main.m
disp('Enter elements of matrix 2: ');
for i = 1:rows2
    for j = 1:cols2
        matrix2(i, j) = input(sprintf('Enter element at position (%d, %d): ', i, j));
    end
end
172 chars
7 lines
  1. Check if the matrices can be multiplied by comparing the number of columns in the first matrix with the number of rows in the second matrix using an if statement:
main.m
if cols1 == rows2
    % Perform matrix multiplication
else
    disp('Matrices cannot be multiplied.');
end
107 chars
6 lines
  1. If the matrices can be multiplied, perform the matrix multiplication using nested for loops:
main.m
result_matrix = zeros(rows1, cols2);
for i = 1:rows1
    for j = 1:cols2
        for k = 1:cols1
            result_matrix(i, j) = result_matrix(i, j) + matrix1(i, k) * matrix2(k, j);
        end
    end
end
208 chars
9 lines
  1. Display the result matrix:
main.m
disp('Result of matrix multiplication: ');
disp(result_matrix);
64 chars
3 lines

Putting all the code snippets together, the complete program would look like this:

main.m
rows1 = input('Enter the number of rows for matrix 1: ');
cols1 = input('Enter the number of columns for matrix 1: ');
matrix1 = zeros(rows1, cols1);

rows2 = input('Enter the number of rows for matrix 2: ');
cols2 = input('Enter the number of columns for matrix 2: ');
matrix2 = zeros(rows2, cols2);

disp('Enter elements of matrix 1: ');
for i = 1:rows1
    for j = 1:cols1
        matrix1(i, j) = input(sprintf('Enter element at position (%d, %d): ', i, j));
    end
end

disp('Enter elements of matrix 2: ');
for i = 1:rows2
    for j = 1:cols2
        matrix2(i, j) = input(sprintf('Enter element at position (%d, %d): ', i, j));
    end
end

if cols1 == rows2
    result_matrix = zeros(rows1, cols2);
    for i = 1:rows1
        for j = 1:cols2
            for k = 1:cols1
                result_matrix(i, j) = result_matrix(i, j) + matrix1(i, k) * matrix2(k, j);
            end
        end
    end
    disp('Result of matrix multiplication: ');
    disp(result_matrix);
else
    disp('Matrices cannot be multiplied.');
end
1031 chars
37 lines

You can copy and paste the code into MATLAB for execution. Note that this program assumes that the user will input valid matrix dimensions and elements.

gistlibby LogSnag