Question

In: Mechanical Engineering

create a function in matlab that sums two m x n matrices using nested loops, then...

create a function in matlab that sums two m x n matrices using nested loops, then returns result into a new matrix. Use nesed for loops to add matrices piece by piece. Basically means, dont program simply A+B

Function should perform error check to make sure both matrices have same number of rows/ columns.

Solutions

Expert Solution

clc;

clear all;

A=input('Enter matrix A');

B=input('Enter matrix B');

[m1 n1]=size(A);

[m2 n2]=size(B);

if(m1==m2 && n1==n2)

for i=1:m1

for j=1:n1

C(i,j)=A(i,j)+B(i,j);

end

end

fprintf('Sum of matrix A & B =');

display(C)

else

fprintf('ERROR--Size of matrix is not same . Can not perform addition')

end

TEST CODE EXAMPLE 1-------

OUTPUT1

Enter matrix A> [1 2;1 2]
Enter matrix B> [1 2 3;1 2 3;1 2 3]
ERROR--Size of matrix is not same . Can not perform addition
 
TEST CODE EXAMPLE 2
OUTPUT
Enter matrix A> [1 2 3;1 2 3;1 2 3]
 Enter matrix B> [3 2 1;3 2 1;3 2 1] 
Sum of matrix A & B =C = 
4 4 4
4 4 4 
4 4 4

Related Solutions

Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
2. Using matrices, create an algorithm that takes a matrix of dimension N x N and...
2. Using matrices, create an algorithm that takes a matrix of dimension N x N and feed it in a spiral shape with the sequential number from 1 to N^2. Then do an algorithm in PSEint
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n'...
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n' (nxn chart).
a. Using Matlab scripts create the following matrices (???1 and ???2) ???1 = [ 3 2...
a. Using Matlab scripts create the following matrices (???1 and ???2) ???1 = [ 3 2 −3 6 7 4 3 −6 7 ], ???2 = [ 2 1 7 3 3 9 −6 6 1 ]    b. Write code to add the second row of ???1 to the third row of ???2 and store results in the first row of ???1. c. Write code to add the second column of ???1 with the third column of ???2 and...
In C 1- Use nested for loops to create an addition lookup table. Fill in an...
In C 1- Use nested for loops to create an addition lookup table. Fill in an array and print out the following table showing the sum of the row and column numbers 0-9. 0 1 2 3 4.. <- column numbers 1 2 3 4 5.. 2 3 4 5 6.. <- These nos. = Row + Column 3 4 5 6 7.. <- for example 7 = 3 + 4 4 5 6 7 8.. ... ^Row numbers 2-...
Given two 2x2 matrices: M=1     21     1,N=-1       2     1   -1 (a).Verify multiplication of M x N...
Given two 2x2 matrices: M=1     21     1,N=-1       2     1   -1 (a).Verify multiplication of M x N mod 26 = N x M mod 26 = I (identity matrix) (b). Use Hill cipher to encrypt the message EXAMS
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
using matlab, compute and plot y [n] = x [n]* h [n], where a. x [n]...
using matlab, compute and plot y [n] = x [n]* h [n], where a. x [n] = h [n] = a^n (0 <=n <=40) & a = 0.5 b. x [n] = cos [n]; h [n] = u [n]; n = 0:4:360 c. x [n] = sin [n] ; h [n] = a^n; n:4:360; a = 0.9
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
Suppose that every row of M sums to k. Prove that M^n has constant row sums,...
Suppose that every row of M sums to k. Prove that M^n has constant row sums, and find that row sum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT