MATLAB PROGRAMMING Your task is to implement a function whic
MATLAB PROGRAMMING
Your task is to implement a function which detects if two n-dimensional rectangles have a non-empty intersection. This question is very similar to the interval intersect problem.
Create a function with signature function z = collide(a1, a2, b2, b2).
All inputs are vectors of the same length n for some n > 0.
Rectangle A has lower-left corner a1 and upper-right corner a2.
Rectangle B has lower-left corner b1 and upper-right corner b2.
The z output is 0 when the intersection is empty, otherwise it is 1.
Examples
It may help you to sketch these and some other on paper.
1D examples:
>> a1 = 1; a2 = 5; b1 = 6; b2 = 20; collide(a1, a2, b1, b2)
ans =
0
>> a1 = 1; a2 = 5; b1 = 2; b2 = 20; collide(a1, a2, b1, b2)
ans =
1
Hints
If you\'re stuck then try implementing the functions as if all of the arguments were scalars. The follow functions may be useful.
>> help min
>> help max
>> help all
>> help any
MATLAB PROGRAMMING
Your task is to implement a function which detects if two n-dimensional rectangles have a non-empty intersection. This question is very similar to the interval intersect problem.
Create a function with signature function z = collide(a1, a2, b2, b2).
All inputs are vectors of the same length n for some n > 0.
Rectangle A has lower-left corner a1 and upper-right corner a2.
Rectangle B has lower-left corner b1 and upper-right corner b2.
The z output is 0 when the intersection is empty, otherwise it is 1.
Examples
It may help you to sketch these and some other on paper.
1D examples:
>> a1 = 1; a2 = 5; b1 = 6; b2 = 20; collide(a1, a2, b1, b2)
ans =
0
>> a1 = 1; a2 = 5; b1 = 2; b2 = 20; collide(a1, a2, b1, b2)
ans =
1
Hints
If you\'re stuck then try implementing the functions as if all of the arguments were scalars. The follow functions may be useful.
>> help min
>> help max
>> help all
>> help any
MATLAB PROGRAMMING
Your task is to implement a function which detects if two n-dimensional rectangles have a non-empty intersection. This question is very similar to the interval intersect problem.
Create a function with signature function z = collide(a1, a2, b2, b2).
All inputs are vectors of the same length n for some n > 0.
Rectangle A has lower-left corner a1 and upper-right corner a2.
Rectangle B has lower-left corner b1 and upper-right corner b2.
The z output is 0 when the intersection is empty, otherwise it is 1.
Examples
It may help you to sketch these and some other on paper.
1D examples:
>> a1 = 1; a2 = 5; b1 = 6; b2 = 20; collide(a1, a2, b1, b2)
ans =
0
>> a1 = 1; a2 = 5; b1 = 2; b2 = 20; collide(a1, a2, b1, b2)
ans =
1
Hints
If you\'re stuck then try implementing the functions as if all of the arguments were scalars. The follow functions may be useful.
>> help min
>> help max
>> help all
>> help any
Solution
Code:
function z = collide(a1,a2,b1,b2)
% find maximum of left corner
max1 = max(a1,b1);
% find minimum of right corner
min1 = min(a2,b2);
if max1 < min1
z=1
else
z=0
end
end

