Assignment 6
Assignment 6
Assignment 6
Note: You should not be in possession of any implementation related to the assignment, other
than your own. In case of any confusion please reach out to the TA.
b = 9;
c = 2*a+b^2-a*b+b/a-10
Output:
53
Example 2
a = 3;
Square_equals = a*a
Cube_equals = a*a*a
square_root = sqrt(a)
Output
Square_equals = 9
Cube_equals = 27
square_root = 1.7321
Example 3
a = 3;
Square_equals = a^2
Cube_equals = a^3
square_root = a^(0.5)
Output
Square_equals = 9
Cube_equals = 27
square_root = 1.7321
b = [1 9 4];
c=a+b
Note: Both a and b are given as a three-element array. In the third line, the operation of "a+b" results
in element-by-element addition
Output
4 15 11
b = [1 9 4 5];
c = a(2) + b(4)
Output
c = 11
y = sin(pi/2)
z=
Output:
x = 3.1416
y=1
z = 0.3679
Basic looping
The for loop
Example. 7 Loop: Using for command
b = 3;
for k = 1:5
end
0utput
3
for k = 1:5
b^k
end
output
3
27
81
243
for k = 1:9
sum1 = sum1+k;
end
sum1
Output:
45
for k = 1:2:9
sum1 = sum1+k;
end
sum1
Output
25
sum1 = 0;
for k = 1:4
sum1 = sum1+b(k);
end
sum
Output
24
Example. 11 Treatment of array within a loop
b = [3 8 9 4 7 5];
sum1 = 0;
for k = 1:2:5
sum1 = sum1+b(k);
end
sum1
output
19
for n = 1:2
for m = 1:3
sum1 = sum1+n*m;
end
end
sum1
output
18
next step
n= 1
m= 2
next step
n= 1
m= 3
next step
n= 2
m= 1
next step
n= 2
m= 2
next step
n= 2
m= 3
c = [2 3 5 7];
sum1 = 0;
for k = 1:4
sum1 = sum1+b(c(k));
end
sum1
output
24
if (num1 > 5)
else
end
Output:
7 is greater than 5
Output:
3 is less than or equal to 5
sum2 = 0;
N=9
for k = 1:N
sum1 = sum1+k;
if (mod(k,3) == 0)
sum2 = sum2+k;
end
end
sum1
sum2
Output:
sum1 = 45
sum2 = 18
b = [3 7 4];
c = a+b
Output:
c = 5 19 29
b = [5 2; 11 7];
c = a+b
Output
c=86
12 13
b = 2*a
Output:
b = 6 10
28
Example. 20 Element-by-element multiplication involving two 1-D arrays or two matrices of the
same dimension
a = [2 3 5];
b = [2 4 9];
b = [5 1; 7 2];
c = a*b
Output:
c = 31 8
33 9
c = A(2,2)+A(1,2)
Output:
c=9
sum = 0;
for m = 1:size(A,1)
for n = 1:size(A,2)
sum = A(m,n)+sum;
end
end
sum
Output
sum=14
Tasks
2. Read two images convert them to gray scale. And perform following tasks and show the results,
0.8 × 𝑖𝑚𝑎𝑔𝑒1 + 0.2 × 𝑖𝑚𝑎𝑔𝑒2
0.7 × 𝑖𝑚𝑎𝑔𝑒1 + 0.3 × 𝑖𝑚𝑎𝑔𝑒2
0.5 × 𝑖𝑚𝑎𝑔𝑒1 + 0.5 × 𝑖𝑚𝑎𝑔𝑒2
𝑖𝑚𝑎𝑔𝑒1 − 𝑖𝑚𝑎𝑔𝑒2
𝐼𝑚𝑎𝑔𝑒2 – 𝑖𝑚𝑎𝑔𝑒2