Question

MATLAB: Do the following with the provided .m file (b) Now on the MATLAB prompt, let...

MATLAB:

Do the following with the provided .m file

(b) Now on the MATLAB prompt, let us create any two 3 × 3 matrices and you can do the following:

X=magic(3);

Y=magic(3);

X*Y

matrixMultiplication3by3(X,Y)

(c) Now write a new function in MATLAB called matrixMultiplication that can multiply any two n × n matrix. You can safely assume that we will not test your program with matrices that do not have their inner dimensions matched up

CODE:

function [C] = matrixMultiplicationFor3by3(A,B)

b1=B(:,1);

b2=B(:,2);

b3=B(:,3);

a1=A(:,1);

a2=A(:,2);

a3=A(:,3);

c1=a1*b1(1)+a2*b1(2)+a3*b1(3);

c2=a1*b2(1)+a2*b2(2)+a3*b2(3);

c3=a1*b3(1)+a2*b3(2)+a3*b3(3);

C=[c1 c2 c3];

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

b)

function SQUARE(n)
x=magic(n);
y=magic(n);
z=x*y;
disp('x value is:')
disp(x)
disp('y value is:')
disp(y)
disp('z value is:')
disp(z)
end

OUTPUT:

>> SQUARE(3)
x value is:
     8     1     6
     3     5     7
     4     9     2

y value is:
     8     1     6
     3     5     7
     4     9     2

z value is:
    91    67    67
    67    91    67
    67    67    91

>>

c)

CODE:

function matrixmultipilcation(n)
x=magic(n)
y=magic(n)
[Rx, Cx] = size(x);
[Ry, Cy] = size(y);
MatrixProduct = zeros(Rx, Cy);
for row = 1 : Rx
for col = 1 : Cy
    S= 0;
    for k = 1 : Cx
      S= S+ x(row, k) * y(k, col);
    end
    MatrixProduct(row, col) = S;
end
end
disp('matrix product')
disp(MatrixProduct)

OUTPUT:

>> matrixmultiplication(4)

x =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1


y =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

matrix product
   345   257   281   273
   257   313   305   281
   281   305   313   257
   273   281   257   345

>>

Add a comment
Know the answer?
Add Answer to:
MATLAB: Do the following with the provided .m file (b) Now on the MATLAB prompt, let...
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
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