0% found this document useful (0 votes)
13 views4 pages

For Loop Practical Example-2

The document outlines the creation of a MATLAB function named findCoprimes that takes a vector of natural numbers and returns a matrix of coprime pairs. It uses nested loops to compare each unique pair of numbers, utilizing the gcd function to determine coprimality. The final output is a matrix where each row represents a pair of coprime numbers.
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)
13 views4 pages

For Loop Practical Example-2

The document outlines the creation of a MATLAB function named findCoprimes that takes a vector of natural numbers and returns a matrix of coprime pairs. It uses nested loops to compare each unique pair of numbers, utilizing the gcd function to determine coprimality. The final output is a matrix where each row represents a pair of coprime numbers.
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

🔢 Function Goal Recap

You're asked to write a MATLAB function that:

●​ Takes a vector x of natural numbers as input.


●​ Returns a matrix y, where each row is a pair of coprime numbers from x.
●​ You are not allowed to use iscoprime, but you can use gcd.

🧠 Code Walkthrough
matlab
CopyEdit
function y = findCoprimes(x)

●​ This defines a new function named findCoprimes.


●​ It takes a vector x (e.g., [2, 3, 4]) as input.
●​ It will return a matrix y with 2 columns: each row is a pair of coprime numbers.

matlab
CopyEdit
y = []; % Initialize empty output matrix

●​ Initializes y as an empty matrix.


●​ This is where we’ll store all coprime pairs we find.

matlab
CopyEdit
for i = 1:length(x)

●​ Starts the outer loop.


●​ i will loop over each element position in the input vector x.

matlab
CopyEdit
for j = i+1:length(x)

●​ Inner loop: j starts at i+1 to avoid:


○​ Comparing the same number with itself
○​ Checking both (a,b) and (b,a), which would be redundant

👉 This ensures we only check unique unordered pairs.


matlab
CopyEdit
if gcd(x(i), x(j)) == 1

●​ Uses gcd() to compute the greatest common divisor of x(i) and x(j).
●​ If the result is 1, then the two numbers are coprime.
●​ Example: gcd(4, 5) = 1 → 4 and 5 are coprime.

matlab
CopyEdit
y = [y; x(i), x(j)]; % Append coprime pair as a row

●​ Appends the pair [x(i), x(j)] as a new row at the bottom of matrix y.
●​ This builds up a full list of coprime pairs.

matlab
CopyEdit
end
end
end

●​ Closes the if, for, and nested for loops.


●​ All pairs have now been checked by the time these finish.

✅ Example Run:
matlab
CopyEdit
x = [2, 3, 4];

What happens step-by-step:

1.​ i = 1 (x(1) = 2)
○​ j = 2 → x(2) = 3 → gcd(2,3) = 1 → coprime → add [2 3]
○​ j = 3 → x(3) = 4 → gcd(2,4) = 2 → not coprime
2.​ i = 2 (x(2) = 3)
○​ j = 3 → x(3) = 4 → gcd(3,4) = 1 → coprime → add [3 4]

Final output:

matlab
CopyEdit
y =
2 3
3 4

✅ Summary of What You Learned:


●​ gcd(a, b) is used to check coprimality.
●​ for i = 1:length(x), for j = i+1:length(x) ensures unique pairs.
●​ y = [y; a, b] builds up the matrix row by row.

FURTHER EXPLAINED

🔄 Why Use Two Nested Loops?


You want to compare every pair of values in the vector x, but you only need each unique
combination once.

🔁 for i = 1:length(x)
This loop selects the first number in each pair (call it x(i)).

🔁 for j = i+1:length(x)
This loop selects the second number (call it x(j)), but starts at i+1 to:

●​ Avoid comparing a number with itself (e.g., gcd(x(1), x(1)))


●​ Avoid checking the same pair twice (e.g., both [2 3] and [3 2])

Example:

If x = [2 3 4], the loop will check:

●​ gcd(2,3) ✅
●​ gcd(2,4) ✅
●​ gcd(3,4) ✅

But it won't check:

●​ gcd(2,2), gcd(3,3)... (self-pairs ❌)


●​ gcd(3,2)... (reverse duplicates ❌)

🔎 Why gcd(x(i), x(j)) == 1?


You're testing:

"Do these two numbers have no common factors except 1?"

If yes → coprime → add the pair to the output list.

You might also like