0% found this document useful (0 votes)
72 views10 pages

Assignment 6

This document provides examples of performing various linear algebra operations in MATLAB such as basic math operations, intrinsic math functions, arrays and matrices, looping, and branching. It also includes 3 tasks involving the use of matrices and images: 1) analyzing properties of a magic square matrix, 2) performing linear combinations and subtraction on grayscale images, and 3) mirroring, flipping, and thresholding a grayscale image using indexing and conditional statements.

Uploaded by

Samreen Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views10 pages

Assignment 6

This document provides examples of performing various linear algebra operations in MATLAB such as basic math operations, intrinsic math functions, arrays and matrices, looping, and branching. It also includes 3 tasks involving the use of matrices and images: 1) analyzing properties of a magic square matrix, 2) performing linear combinations and subtraction on grayscale images, and 3) mirroring, flipping, and thresholding a grayscale image using indexing and conditional statements.

Uploaded by

Samreen Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LINEAR ALGEBRA

Assignment 6

Submission deadline: 21-May-2020


Semester: 2nd Semester
Batch: BSEE-19
Assignment outcome matching with: CLO-1
Total marks: 100

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.

Basic Math Operations


Example. 1
a = 3;

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

Example. 4 Arrays addition (Row vector)


a = [3 6 7];

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

Example. 5 Extracting an individual element of an array


a = [3 6 7];

b = [1 9 4 5];

c = a(2) + b(4)

Output
c = 11

Example. 6 Intrinsic math functions and constants


x = pi

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

Example. 8 Loop: Using for command


b = 3;

for k = 1:5

b^k

end

output
3

27
81

243

Example. 8 Loop: Using for command


sum1 = 0;

for k = 1:9

sum1 = sum1+k;

end

sum1

Output:
45

Example.9 Using for command


sum1 = 0;

for k = 1:2:9

sum1 = sum1+k;

end

sum1

Output
25

Example. 10 Treatment of array within a loop


b = [3 8 9 4 7 5];

sum1 = 0;

for k = 1:4

sum1 = sum1+b(k);

end

sum

Note: It is also called as sum of all elements of roe vector b

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

Example. 12 Double loop


sum1 = 0;

for n = 1:2

for m = 1:3

sum1 = sum1+n*m;

end

end

sum1

output
18

Example. 13 Double loop


for n = 1:2
for m = 1:3
n
m
printf('next step\n')
end
end
output
n= 1
m= 1

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

Example. 14 More complicated use of loop and index


b = [2 5 7 4 9 8 3];

c = [2 3 5 7];

sum1 = 0;

for k = 1:4

sum1 = sum1+b(c(k));

end

sum1

output
24

Note: This program performs the summation of


sum1 = b (c (1)) +b (c (2)) +b (c (3)) +b (c (4))
= b (2) +b (3) +b (5) +b (7)
= 5+7+9+3
= 24
Basic branching
Example. 15 Basic branching
num1 = 7;

if (num1 > 5)

fprintf('%4u is greater than 5 \r', num1)

else

fprintf('%4u is less than or equal to 5 \r', num1)

end

Output:
7 is greater than 5

Same program, but change first line to "num1 = 3;"

Output:
3 is less than or equal to 5

Example. 16 Combine looping and branching


sum1 = 0;

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

Note: Sum1 = 1+2+3+4+5+6+7+8+9, while sum2 = 3+6+9.

Array and Matrix

Example. 17 Assign the content of a (one-dimensional) array; Addition of two arrays


a = [2 12 25];

b = [3 7 4];

c = a+b

Output:
c = 5 19 29

Example. 18 Assign the content of a matrix; Addition of two matrices


a = [3 4; 1 6];

b = [5 2; 11 7];

c = a+b

Output
c=86

12 13

Example. 19 Multiplication involving a scalar and an array (or a matrix)


a = [3 5; 1 4];

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];

c = a.*b % Using dot before operator results in element wise operations


Output:
c = 4 12 45

Example. 21 Direct (not element-by-element) multiplication of two matrices


a = [2 3; 1 4];

b = [5 1; 7 2];

c = a*b

Output:
c = 31 8

33 9

Example.22 Extracting the individual element(s) of a matrix


A = [3 5; 2 4];

c = A(2,2)+A(1,2)

Output:
c=9

Example.23 Usage of index for a matrix


A = [3 5; 2 4];

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

1. Let’s consider a matrix such that,


𝐴 = 𝑚𝑎𝑔𝑖𝑐(10,10)
 What is the minimum value in each column of A?
 What is the average of all diagonal elements?
 What is the sum of all the odd index of A?

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

3. Read image3 convert it to gray scale,


 Mirror the image using for loop and matrix indexing
 Flip image upside down using for loop and matrix indexing
 Using matrix indexing and condition statement replace each element of image with
each value greater than 160 with 1 else zero.

You might also like