0% found this document useful (0 votes)
5 views9 pages

Lecture 3

The document provides an overview of relational operations in MATLAB, detailing key relational operators and their usage for comparing values in scalars, vectors, matrices, and arrays. It includes examples of basic comparisons, combining relational operators with logical operators, and applications such as array indexing and filtering. Additionally, it covers advanced topics like matrix indexing, summing odd numbers, and finding maximum values in matrices.

Uploaded by

Hiba Al-Qaisy
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)
5 views9 pages

Lecture 3

The document provides an overview of relational operations in MATLAB, detailing key relational operators and their usage for comparing values in scalars, vectors, matrices, and arrays. It includes examples of basic comparisons, combining relational operators with logical operators, and applications such as array indexing and filtering. Additionally, it covers advanced topics like matrix indexing, summing odd numbers, and finding maximum values in matrices.

Uploaded by

Hiba Al-Qaisy
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/ 9

1.

Introduction to Relational Operations


Relational operations in MATLAB are used to compare two values or expressions and return a
logical result (true or false). The decision of 1 means true and 0 means false.

Key Relational Operators in MATLAB

Operator Description Example

== Equal to A == B

~= Not equal to A ~= B

> Greater than A > B

< Less than A < B

>= Greater than or equal A >= B

<= Less than or equal A <= B

2. Using Relational Operators


2.1 Basic Comparisons

Relational operations can be applied to scalars, vectors, matrices, and arrays.

Example 1: Scalar Comparison

a = 5;
b = 3;
result = (a > b); % Returns true (1)
disp(result); % Output: 1

Example 2: Vector Comparison

x = [1, 5, 3];
y = [2, 5, 1];
result = (x == y); % Returns [0, 1, 0]
disp(result); % Output: [0 1 0]
Example 3: Matrix Comparison

A = [1 2; 3 4];
B = [1 0; 3 5];
result = (A >= B); % Returns [1 1; 1 0]
disp(result);

2.2 Combining Relational Operators with Logical Operators

MATLAB supports logical operators (& (AND), | (OR), ~ (NOT)) to combine multiple conditions.

Example 4: Using & (AND) and | (OR)

x = 10;
condition = (x > 5) & (x < 15); % true (1)
disp(condition); % Output: 1

condition2 = (x < 5) | (x == 10); % true (1)


disp(condition2); % Output: 1

Example 5: Using ~ (NOT)

a = 7;
b = 10;
result = ~(a == b); % true (1)
disp(result); % Output: 1

3. Applications of Relational Operations


3.1 Array Indexing and Filtering

Relational operations help filter data in arrays.

Example 6: Filtering Values in an Array

data = [3, 8, 1, 6, 4];


filtered = data(data > 4); % Returns [8, 6]
disp(filtered); % Output: [8 6]
3.2 Vectorized Operations Over Loops

MATLAB is optimized for vectorized operations. Avoid loops when possible.

Example 10: Vectorized Relational Operation

A = [1, 2, 3];
B = [2, 2, 2];
result = (A <= B); % [1, 1, 0]

Extra Examples:

MATLAB code segment that generates a random vector of size 20 and performs various filtering
operations, finding:

• Numbers at even indices


• Numbers at odd indices
• Positive numbers
• Even numbers
• Numbers between 5 and 15
• Non-zero numbers

% Generate a random vector of size 20 (including negative and positive numbers)


vec = randi([-10, 20], 1, 20);

disp('Original Vector:');
disp(vec);

%% 1. Numbers at Even Indices (2nd, 4th, 6th, ...)


even_indices = vec(2:2:end);
disp('Numbers at Even Indices:');
disp(even_indices);

%% 2. Numbers at Odd Indices (1st, 3rd, 5th, ...)


odd_indices = vec(1:2:end);
disp('Numbers at Odd Indices:');
disp(odd_indices);

%% 3. Positive Numbers
positive_nums = vec(vec > 0);
disp('Positive Numbers:');
disp(positive_nums);

%% 4. Even Numbers
even_nums = vec(mod(vec, 2) == 0);
disp('Even Numbers:');
disp(even_nums);

%% 5. Numbers Between 5 and 15 (inclusive)


nums_between_5_15 = vec(vec >= 5 & vec <= 15);
disp('Numbers Between 5 and 15:');
disp(nums_between_5_15);

%% 6. Non-Zero Numbers
non_zero_nums = vec(vec ~= 0);
disp('Non-Zero Numbers:');
disp(non_zero_nums);

%% Additional Useful Filtering Examples

% 7. Numbers Greater Than 10


greater_than_10 = vec(vec > 10);
disp('Numbers Greater Than 10:');
disp(greater_than_10);

% 8. Negative Numbers
negative_nums = vec(vec < 0);
disp('Negative Numbers:');
disp(negative_nums);

% 9. Numbers Divisible by 3
divisible_by_3 = vec(mod(vec, 3) == 0);
disp('Numbers Divisible by 3:');
disp(divisible_by_3);

% 10. Numbers Equal to a Specific Value (e.g., 7)


equal_to_7 = vec(vec == 7);
disp('Numbers Equal to 7:');
disp(equal_to_7);

Explanation of Each Operation


1. Even Indices (2:2:end)

o Extracts elements at positions 2, 4, 6, ..., 20.


2. Odd Indices (1:2:end)
o Extracts elements at positions 1, 3, 5, ..., 19.
3. Positive Numbers (vec > 0)

o Uses logical indexing to select elements greater than 0.


4. Even Numbers (mod(vec, 2) == 0)

o Checks divisibility by 2 to find even numbers.


5. Numbers Between 5 and 15 (vec >= 5 & vec <= 15)

o Combines two conditions using & (logical AND).


6. Non-Zero Numbers (vec ~= 0)

o Excludes all zeros from the vector.


7. Numbers Greater Than 10 (vec > 10)
8. Negative Numbers (vec < 0)
9. Numbers Divisible by 3 (mod(vec, 3) == 0)
10. Numbers Equal to a Specific Value (vec == 7)

Examples from Lab Lecture:


arr_1 = randi(50,[1 10])
%arr_2 = randi(50,[1 10])
aa=arr_1(arr_1>5 & mod(arr_1,2)==0)
ar = arr_1(arr_1 == arr_2)
odd_no=arr_1(mod(arr_1,2)~=0)
even_no=arr_2(mod(arr_2,2)==0)
com=[odd_no, even_no]
nm=[arr_1(mod(arr_1,2)~=0),arr_2(mod(arr_2,2)==0)]

Explanation:
Purpose: Creates a 1×10 row vector of random integers between 1 and 50.
arr_1 = randi(50,[1 10])

Extracts numbers from arr_1 that are:


Greater than 5: (arr_1 > 5) and Even: (mod(arr_1, 2) == 0)
aa=arr_1(arr_1>5 & mod(arr_1,2)==0)

Logical Conditions:

arr_1 > 5 → [1, 1, 1, 0, 1, 1, 1, 1, 1, 1] (logical mask)

mod(arr_1, 2) == 0 → [0, 1, 0, 0, 1, 0, 1, 0, 1, 0] (logical mask)

Combined with & → [0, 1, 0, 0, 1, 0, 1, 0, 1, 0]


aa = [22, 40, 8, 36]
ar = arr_1(arr_1 == arr_2)
Finds elements in arr_1 that are equal to the corresponding elements in arr_2.
• Example:

o If arr_1 = [7, 22, 15] and arr_2 = [7, 5, 15],


o arr_1 == arr_2 → [1, 0, 1]

o ar = [7, 15]

odd_no=arr_1(mod(arr_1,2)~=0)
Extracts odd numbers from arr_1.
Condition: mod(arr_1, 2) ~= 0 → [1, 0, 1, 1, 0, 1, 0, 1, 0, 1]
Result (odd_no): odd_no = [7, 15, 3, 11, 29, 17]

arr_2 = randi(50,[1 10])


even_no=arr_2(mod(arr_2,2)==0)

Extracts even numbers from arr_2.


• Condition: mod(arr_2, 2) == 0 → Even numbers.
• Example:

o If arr_2 = [4, 9, 16, 5, 20],


o even_no = [4, 16, 20]

com=[odd_no, even_no]

Combines odd_no (from arr_1) and even_no (from arr_2) into a single array.
• Example:

o If odd_no = [7, 15, 3] and even_no = [4, 16, 20],


o com = [7, 15, 3, 4, 16, 20]

nm=[arr_1(mod(arr_1,2)~=0),arr_2(mod(arr_2,2)==0)]
Does the same as com but without intermediate variables.

• Equivalent to: nm = [odd_numbers_from_arr_1, even_numbers_from_arr_2]

• Example:

o If arr_1 = [7, 22, 15] and arr_2 = [4, 9, 16],


o nm = [7, 15, 4, 16]
Review of lecture 2:

A = randi([1 20], [7 10]);


disp(A)

1. Matrix Indexing and Subsetting


(a) Selecting Specific Rows and Columns
bb = A([1 4 7], [2 4 7])

• Purpose: Creates a submatrix from A containing:

o Rows 1, 4, and 7
o Columns 2, 4, and 7
• Example:

o If A is:
3 14 8 19 2 11 7 15 9 12
17 5 12 6 18 10 16 4 13 1
19 7 1 11 5 14 3 20 8 10
6 16 10 4 12 9 15 2 17 18
11 2 18 8 7 13 5 12 14 6
4 9 3 17 10 20 1 19 15 7
20 1 13 5 16 6 12 8 4 11

o Then bb will be:


14 19 7
16 4 15
1 5 12

2. Sum of Odd Numbers


S = sum(A(mod(A, 2) ~= 0))

• Purpose: Computes the sum of all odd numbers in matrix A.


• How it works:

1. mod(A, 2) ~= 0 creates a logical mask where 1 represents odd numbers.


2. A(mod(A, 2) ~= 0) extracts all odd numbers as a column vector.
3. sum() adds them up.
• Example: If A has odd numbers [3, 17, 19, 5, 11, 7, 9, 13, 15, 1, 19, 3, 17, 11,
5, 13, 1, 7, 11], then S is their sum.
3. Maximum Values in Columns and Rows
(a) Maximum in Each Column
x1 = max(A, [], 1)

• Purpose: Finds the maximum value in each column (dimension 1).


• Output: A row vector with 10 elements (one per column).
• Example:
x1 = [20, 16, 18, 19, 18, 20, 16, 20, 17, 18]

(b) Maximum in Each Row + Indices


[M, idx] = max(A, [], 2)

• Purpose: Finds the maximum value in each row (dimension 2), along with its column index.
• Outputs:

o M: Column vector of maximum row values.

o idx: Column vector of column indices where max occurs.

• Example:
M = [19, 18, 20, 18, 18, 20, 20]
idx = [4, 5, 8, 9, 3, 6, 1]

(c) Global Maximum in Matrix


mx = max(A, [], 'all')

• Purpose: Finds the single largest value in the entire matrix.


• Example: mx = 20

4. More Examples of Matrix/Vector Operations


(a) Find All Numbers >10
greater_than_10 = A(A > 10)

• Returns a column vector of elements >10.

(b) Count Even Numbers


num_evens = sum(mod(A, 2) == 0, 'all')

• Counts how many even numbers exist in A.


(c) Extract Diagonal Elements
diag_elements = diag(A)

• Works if A is square. For rectangular matrices, use:


diag_elements = A(1:min(size(A)), 1:min(size(A)))

(d) Find Indices of Specific Values


[row, col] = find(A == 15) % Finds all positions where A equals 15

(e) Compute Row-wise Sums


row_sums = sum(A, 2)

Key Concepts Summary


Concept Syntax Example Purpose

Submatrix A([1 3], [2 4]) Select specific rows/columns

Logical Indexing A(A > 10) Filter elements by condition

Odd/Even mod(A,2)~=0 Identify odd numbers

Column Max max(A,[],1) Maximum in each column

Row Max max(A,[],2) Maximum in each row

Global Max max(A,[],'all') Maximum in entire matrix

Sum Condition sum(A(mod(A,2)==0)) Sum all even numbers

You might also like