Question
Please code in MatLab or Octave
Output should match Sample Output in the second picture
Thank You

4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x
This program will multiply a random mx n matrix by a random nx p matrix Enter a positive integer m: 0 Invalid entry 0 Enter a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB Code:

function [m, n, p] = get_matrix_dimensions()
  
% Printing description
fprintf("\n This program will multiply a random m x n matrix by a random n x p matrix \n");
  
% Getting numbers
m = get_positive_integer('m');
n = get_positive_integer('n');
p = get_positive_integer('p');
  
end # function end

function n = get_positive_integer(ch)
% Function that reads positive integer
  
% Loop control variable
isLoop = true;
  
% Validating n
while isLoop == true
% Prompting user
fprintf("Enter a positive integer %s: ",ch);
n = input("");
  
% Less than 0
if n <= 0
fprintf("Invalid entry %d\n", n);
elseif floor(n)~=n
fprintf("Invalid entry %.1f\n", n);
else
isLoop = false;
end % If end
  
end % While end
  
end % function end

function [A, B] = generate_matrices(m, n, p)
% Generating matrices
A = randi([-5 5], m, n);
B = randi([-5 5], n, p);
end % function end

function matrix3 = multiply_matrices (matrix1, matrix2, m, n, p)
% Matrix Multiplication
% Initializing matrix 3 with 0's
matrix3 = zeros(m, p);
  
% Calculating result
for i = 1:m
for j = 1:p
for k = 1:n
matrix3(i,j) = matrix3(i,j) + ( matrix1(i,k) * matrix2(k,j) );
end
end
end
end % function end

function print_results (A, B, C, m, n, p)
% Function that prints results
fprintf("\nMatrix A is: \n");
for i=1:m
for j=1:n
fprintf("%5d", A(i, j));
end
fprintf("\n");
end

fprintf("\n\nMatrix B is: \n");
for i=1:n
for j=1:p
fprintf("%5d", B(i, j));
end
fprintf("\n");
end
  
fprintf("\n\nThe result of A times B is: \n");
for i=1:m
for j=1:p
fprintf("%5d", C(i, j));
end
fprintf("\n");
end

end % function end

Script:

% Script that handles matrix Multiplication

% Reading dimensions
[m, n, p] = get_matrix_dimensions();

% Generating matrices
[A, B] = generate_matrices(m, n, p);

% Multiplying matrices
C = multiply_matrices(A, B, m, n, p);

% Printing results
print_results(A, B, C, m, n, p);

_______________________________________________________________________________________________________

Sample Run:

>> matMultiplication This program will multiply a random m x n matrix by a random n xp matrix Enter a positive integer m: 0 I

Add a comment
Know the answer?
Add Answer to:
Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please code in MatLab or Octave Output should match Sample Output in Second Picture Thank you...

    Please code in MatLab or Octave Output should match Sample Output in Second Picture Thank you 5. In this problem we will investigate using the Secant Method to approximate a root of a function f(r). The Secant Method is an iterative approach that begins with initial guesses , and r2. Then, for n > 3, the Secant Method generates approximations of a root of f(z) as In-1-In-2 n=En-1-f (x,-1) f(Fn-1)-f(-2) any iteration, the absolute error in the approximation can be...

  • MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and on...

    MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix. 5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...

  • Recall that if A is an m times n matrix and B is a p × q matrix

    Recall that if A is an m times n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p. in which case C is an m × q matrix.  a. Write a function M-file that takes as input two matrices A and B, and as output produces the product by rows of the two matrices. For instance, if A is 3 times 4 and B is...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • Using Matlab or Octave, I do not know how to write the script for this problem,...

    Using Matlab or Octave, I do not know how to write the script for this problem, please help. Must match sample output 5. Consider a 3 x 3 matrix given by A = 01 012 013 az az 633 ( dai 632 033 The trace of A is defined as tr(A)=2au. Given another 3 x 3 matrix bu bı2 bia B- bi brz bra | bgi bz2 by it can be shown that trAB') = tr(AB). Write a script that...

  • answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace...

    answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace all elements divisible by 3 with 100. b) Replace all elements divisible by 2 with 50. c) Replace the first element of each row with 10. d) Display the all elements of the 2D matrix as a 10 array (9 elements in 1 row) 8) Write a function file that determines the maximum element of an input matrix A using nested loops and counts...

  • question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user...

    question 2 in C programming please PE-05b-01 (Six-sider) write a counter-controlled program that prompts the user to enter the number of times n to roll a six-sided die. The program should print to the screen the iteration of the loop and result for each roll 1 ) How many times would you like to roll? 3 roll 6 6 5 1 2) PE 05b 02 (Flash Cards) Write a counter-controlled program that creates addition problems for an elementary kid to...

  • use Python IDEL Problem 4 Fix a program. This program should calculate the matrix addition (C...

    use Python IDEL Problem 4 Fix a program. This program should calculate the matrix addition (C - A+B, where each element of matrix C is equal to the sum of the elements of matrices A and B that have the same indexes cij - aij + bij). However it is incomplete: the range() function arguments, index values of nested lists A, B, and C, and two list statements are missing. Can you find them and fix the program? What is...

  • Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include...

    Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include two things: the PDF and the zipped folder. Late assignments are not accepted. For the questions below, do not create the arrays or matrices by hand. Instead, use built-in MATLAB functions and shorthand notations. Otherwise, no points will be given. 1) Create a function called my product that computes the multiplication of the numbers in any vector using a while loop. The function should...

  • #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function...

    #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function named roll that #accepts an integer argument number_of_throws. The #function should generate and return a sorted #list of number_of_throws random numbers between #1 and 6. The program should prompt the user to #enter a positive integer that is sent to the function, #and then print the returned list. How would you do this?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT