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
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.