Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this, please do not use any built in MatLab functions that automatically calculate the product of matricies or the dot product of two vectors.

For this, please do not use any built in MatLab functions that automatically calculate the product of matricies or the dot product of two vectors.

image text in transcribed

image text in transcribedimage text in transcribed

\%Your task for this lab assignment is to correctly implement \%the matrixmultiply function and the dotproduct function \%based on their comments, pseudocode, provided code, and examples. \%Assume arg1 is the name of an input file in the current working \%directory that contains the elements of the n by matrix A. \%Assume arg2 is the name of an input file in the current working \%directory that contains the elements of the m by r matrix B. \%Assume matricies A and B are compatible for matrix multiplication. \%Since the matrixmultiply function calls the dotproduct function, \%the dotproduct function should be implemented first. \%Do not use any built in functions or operators that automatically \%compute the product (multiplication) of matricies or the dot product. function [P]= matrixmultiply (arg1,arg2) A= readmatrix(arg1); \%initialize A from file B= readmatrix (arg2); \%initialize B from file \%initialize n to the number of rows in A \%initialize m to the number of columns in A \%initialize r to the number of columns in B if (m=size(B,1)) fprintf("Error: Multiplication is undefined for A and B. Goodbye. ") return; end \% use Matlab's display function to display A as shown in the examples \% use Matlab's display function to display B as shown in the examples % print the following as shown in the examples fprintf("Assume AB=P. "); fprintf("Dot product(s) for calculating P: "); % initialize P to an n by r matrix filled with zeros \% by using Matlab's zeros function \%Pseudocode for computing P. \% for each row i in P(i,j) % for each column j in P(i,j) % assign rowA to the ith row in A % assign colB to the jth row in B \% using the dotproduct function that you implented, % assign P(i,j) to the dotproduct of rowA and colB \% print the dot product as shown in the examples by using the code belov %fprintf("P(%i,%i)=",i,j); % printvectors (rowA, colB); % fprintf(" =%.4f ",P(i,j)); \% use Matlab's display function to display P as shown in the examples end \%Return d, the dot product of vectors x and y. \%Use loop(s), variable(s), and simple arithmetic operator(s) to \%to implement this function. You may use the size function. \%Do not use any built in functions or operators that calculate % the dot product automatically. function [d]=dotproduct(x,y) end \%Print a row vector u dot column vector v in a manner consistent \%with the provided examples. \%Do not change this function. function []=printvectors(u,v) \%print u n=size(u,2); fprintf( " [" ); for i=1:n1 fprintf("\%.4f", u(i)); end \%print. fprintf("\%.4f].", u(n)); \%print v fprintf(" ["); m=size(v,1); for i=1:m1 fprintf("\%.4f", v(i)); end fprintf("\%.4f]", v(m)); end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions