Write a MATLAB code that can perform the following and answe

Write a MATLAB code that can perform the following and answer the questions below.

1. Generate an array A of 5x5 elements which elements are 0.147 0.975 0.156 0.719 0.157 0.058 0.785 0.067 0.818 0.035 A= 0.270 0.689 0.372 0.917 0.491 0.134 0.957 0.484 0.722 0.340 0.324 0.964 0.003 0.955 0.178 a. Use imshow to display A b. Find the average of all of its elements c. Find the maximum and minimum number in this array d. How many elements are greater than 0.5? What are they?

2. Generate an array B of 5x5 elements which elements are 0.8147 0.0975 0.1576 0.1419 0.6557 0.0058 0.9785 0.2706 0.8218 0.0357 B= 0.1270 0.2469 0.9572 0.0157 0.1491 0.2134 0.9575 0.4854 0.7922 0.3340 0.6324 0.1649 0.2003 0.4595 0.6787 a. Use imshow to display B b. A.*B and A*B, what is the difference? c. A./B and A/B, what is the difference?

3. For A and B, replace all elements that are greater than or equal to 0.5 with the value of 1, while elements that are less than 0.5 with a 0. Now use imshow to display A and B, comment on the resulting images.

Solution

(1)

A=[0.147 0.975 0.156 .719 .157; .058 .785 0.067 .818 .035;...
.270 .689 .372 .917 .491; .134 .957 .484 .722 .340; .324 .964 .003 .955 .178]

sum=0;
maximum_value=-inf;
minimum_value=inf;
[m n]=size(A);
for i=1:m
for j=1:n
sum=sum+A(i,j);
if A(i,j)>maximum_value
maximum_value=A(i,j);
end
if A(i,j)<minimum_value
minimum_value=A(i,j);
end
end
end
Average_value=sum/(m*n);
fprintf(\'\ Average Value = %.4f\ \',Average_value);
fprintf(\'\ Maximum Value = %.4f\ \',maximum_value);
fprintf(\'\ Minimum Value = %.4f\ \',minimum_value);

count=0;
fprintf(\'\ Elements greater than 0.5 are :\ \');
for i=1:5
for j=1:5
if A(i,j)>0.5
fprintf(\'\\t \\t %.4f \ \',A(i,j));
count=count+1;
end
end
end
fprintf(\'There are %d elements greater than 0.5\ \',count);

Output:

A =
0.1470 0.9750 0.1560 0.7190 0.1570
0.0580 0.7850 0.0670 0.8180 0.0350
0.2700 0.6890 0.3720 0.9170 0.4910
0.1340 0.9570 0.4840 0.7220 0.3400
0.3240 0.9640 0.0030 0.9550 0.1780

Average Value = 0.4687

Maximum Value = 0.9750

Minimum Value = 0.0030

Elements greater than 0.5 are :
      0.9750
      0.7190
      0.7850
      0.8180
      0.6890
      0.9170
      0.9570
      0.7220
      0.9640
      0.9550
There are 10 elements greater than 0.5

(2)

B=[0.8147 0.0975 0.1576 0.1419 0.6557; 0.0058 0.9785 0.2706 0.8218 0.0357; 0.1270 0.2469...
0.9572 0.0157 0.1491; 0.2134 0.9575 0.4854 0.7922 0.3340; 0.6324 0.1649 0.2003 0.4595 0.6787]

fprintf(\'\ A*B=\ \');
A*B
fprintf(\'\ A.*B=\ \');
A.*B
fprintf(\' A*B is a matrix multiplication where as A.*B is a elementwise Multiplication. Elementwise multipliction Possible for matrices with same size but matirx multiplication doesn not\ ;\');

fprintf(\'\ A/B=\ \');
A/B
fprintf(\'\ A./B=\ \');
A./B
fprintf(\' A/B is a matrix Division where as A.*B is a elementwise Division.\ \');

OUTPUT:

B =
0.8147 0.0975 0.1576 0.1419 0.6557
0.0058 0.9785 0.2706 0.8218 0.0357
0.1270 0.2469 0.9572 0.0157 0.1491
0.2134 0.9575 0.4854 0.7922 0.3340
0.6324 0.1649 0.2003 0.4595 0.6787

A*B=
ans =
0.3979 1.7212 0.8168 1.4663 0.5012
0.2570 1.5793 0.6898 1.3185 0.3730
0.7774 1.7514 1.1285 1.5624 0.8966
0.5453 1.8164 1.1619 1.5413 0.6661
0.5863 1.9194 0.8140 1.6766 0.6871

A.*B=
ans =
0.1198 0.0951 0.0246 0.1020 0.1029
0.0003 0.7681 0.0181 0.6722 0.0012
0.0343 0.1701 0.3561 0.0144 0.0732
0.0286 0.9163 0.2349 0.5720 0.1136
0.2049 0.1590 0.0006 0.4388 0.1208
A*B is a matrix multiplication where as A.*B is a elementwise Multiplication. Elementwise multipliction Possible for matrices with same size but matirx multiplication doesn not
;
A/B=
ans =
0.4271 0.3060 -0.3033 0.8335 -0.5410
-0.0708 1.5639 -0.0292 -0.8427 0.4589
-0.7058 0.1857 0.0009 0.3946 1.2012
-0.0983 -0.7915 -0.1660 1.9067 -0.2643
0.5118 2.0029 -0.1402 -1.0960 0.2326

A./B=
ans =
0.1804 10.0000 0.9898 5.0669 0.2394
10.0000 0.8022 0.2476 0.9954 0.9804
2.1260 2.7906 0.3886 58.4076 3.2931
0.6279 0.9995 0.9971 0.9114 1.0180
0.5123 5.8460 0.0150 2.0783 0.2623
A/B is a matrix Division where as A.*B is a elementwise Division.

(3)

for i=1:m
for j=1:n
if A(i,j)>0.5
A(i,j)=1;
else
A(i,j)=0;
end
  
if B(i,j)>0.5
B(i,j)=1;
else
B(i,j)=0;
end
end
end
A
B

OUTPUT:

A =
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
B =
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1

Write a MATLAB code that can perform the following and answer the questions below. 1. Generate an array A of 5x5 elements which elements are 0.147 0.975 0.156 0
Write a MATLAB code that can perform the following and answer the questions below. 1. Generate an array A of 5x5 elements which elements are 0.147 0.975 0.156 0
Write a MATLAB code that can perform the following and answer the questions below. 1. Generate an array A of 5x5 elements which elements are 0.147 0.975 0.156 0

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site