Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note 1: Ensure that you follow MATLAB naming rules for variables and filenames N

ID: 3822048 • Letter: N

Question

Note 1: Ensure that you follow MATLAB naming rules for variables and filenames
Note 2: Insert comments wherever appropriate for clarity of your program
Note 3: Your script must have no syntax errors; if it fails to run or if errors (with red messages on the Command Window) are generated then no points will be given
Note 4: Each problem should have a comments section that describes what that script does
Note 5: Each part should start with clc,clear as the first instruction of that section

2. (40 Points) Using MATLAB loops and the size0 function, create a MATLAB script that will multiply 2 matrices. Note that size(A) will return a row vector containing the dimensions of a matrix A. Thus, you need to code a statement such as Irowsize, colsizeE size(A). You may not multiply matrices A and B just using A B: you MUST use matrix indexing to compute the matrix product as if you were multiplying the matrices using pencil and paper. ADDITION: If the two matrices cannot be multiplied together, display an error and do not compute any voltage values

Explanation / Answer

a = [[1 2 3];[4 5 6]; [7 8 9]] %input matrix a
b = [[1 0 0]; [0 1 0]; [0 0 1]] %input matrix b


[row_a col_a] = size(a); %get size of input matrix a
[row_b col_b] = size(b); %get size of input matrix b

if col_a~=row_b %check multiplication compatibility
    fprintf("The matrix a and b are not matrix compatable "); %if not campatable print error
    return; %and stop
end
c = zeros([row_a col_b]); %else create the resultant matrix and initialize all element to zero

%given below three nexted loop is a standrad looping construct used for matrix multilication

for i = 1:row_a
    for j = 1:col_b
        for k = 1:col_a
            c(i,j) = c(i,j) + a(i,k)*b(k,j); %multiply and add to generate the (
        end
    end
end
c %print the reslt matrix

As requested I kept the code in script form and not in a function form. I also kept the code simple and straightforward as possible, and also commented almost every line of the code to make things easy. I hope you understand the logic and working of the code. In case you still face any problem with the code, please comment below. I shall be glad to help you with the code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote