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

Free

70.2K

Verified Solution

Question

Mechanical Engineering

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

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

Answer & Explanation Solved by verified expert
4.1 Ratings (530 Votes)

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

Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students