What information is stored by p1/p2/... in this code sample?
Show older comments
Dear Community,
I'm going through a couple of sample Matlab programs to get familiar with the syntax, but I have trouble understanding the following line.
p1 = (A(:,1) * point1(1) + A(:,2) * point1(2)) - b;
What information is stored by this p1? Is this something specific from the mathematical standpoint? Looking at the syntax, my understanding is that the 1st column of A is multiplied by the 1st coordinate of point1 + 2nd column of A is multiplied by 2nd coordinate of point1 - vector b.
But I can't understand what information is represented by this p1. Is it some distance/norm?
Full code:
A = [ -0.3 1; 4 1; 1.1 1];
b = [-6 -10 4]';
x_low = -15; x_high = 20;
y_low = (b - A(:,1)*x_low)./A(:,2);
y_high = (b - A(:,1)*x_high)./A(:,2);
n = size(A, 1);
X = [x_low; x_high]*ones(1, n);
Y = [y_low' ; y_high'];
figure(1), line(X,Y), grid;
axis([-15 25 -25 15]);
point1 = [5; 5];
point2 = [-5; 0];
point3 = [5; -10];
point4 = [0; 0];
p1 = zeros(1, 3); p2 = zeros(1, 3); p3 = zeros(1, 3); p4 = zeros(1, 3);
p1 = (A(:,1) * point1(1) + A(:,2) * point1(2)) - b;
disp(p1);
p2 = (A(:,1) * point2(1) + A(:,2) * point2(2)) - b;
p3 = (A(:,1) * point3(1) + A(:,2) * point3(2)) - b;
p4 = (A(:,1) * point4(1) + A(:,2) * point4(2)) - b;
hold on,
plot(point1(1),point1(2),'r*');
plot(point2(1),point2(2),'r*');
plot(point3(1),point3(2),'r*');
plot(point4(1),point4(2),'ro');
hold off
3 Comments
Stephen23
on 2 Dec 2024
"But I can't understand what's the usage of this information."
The information is not used for anything: the variables p1, p2, p3 and p4 are defined and then not used in the code you have shown. They do nothing.
Thomas
on 2 Dec 2024
dpb
on 2 Dec 2024
Better go ask the person who wrote it...
Accepted Answer
More Answers (1)
Govind KM
on 2 Dec 2024
Hi Thomas,
I believe the provided code represents a system of linear inequalities, with A and b representing the coefficients and constants of the linear inequalities respectively. Your understanding of the mentioned line
p1 = (A(:,1) * point1(1) + A(:,2) * point1(2)) - b;
is correct, in that the columns of A are mutiplied with the corresponding coordinate of the point (point1 in this case), and b is subtracted from this result.
For certain operations, MATLAB implicitly expands arrays with compatible sizes to be the same size during the execution of the operation. Hence, this line of code computes the expression A(:,1)x + A(:,2)y - b for all three inequalities through implicit expansion, considering x and y to be the coordinates of point1. The result is a measure of how much point1 satisfies each of the three inequalities. This evaluates to zero if the point lies exactly on the line, a negative value if the point satisfies the inequality, and a positive value if it violates the inequality. Similiar calculations are performed for the other points.
More information on implicit array expansion can be found in the following documentation:
Hope this clarifies the issue!
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!