What information is stored by p1/p2/... in this code sample?
조회 수: 8 (최근 30일)
이전 댓글 표시
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
채택된 답변
John D'Errico
2024년 12월 2일
편집: John D'Errico
2024년 12월 3일
Undocumented, uncommented code, from an unknown source? Shiver.
No. It is not a distance computation. That much I can say.
The problem is, arrays and vectors mean nothing by themselves. They just contain numbers. Out of context, they are virtually meaningless. I don't wish to insult the person who wrote the code, but it is not even terribly good code. For example, they might have written this instead:
A = [ -0.3 1; 4 1; 1.1 1];
b = [-6 0 4]';
point1 = [5; 5];
p1 = A*point1 - b;
Better code yet would have stored all of the "points" in one array.
points = [5 -5 5 0;5 0 -10 0];
Now they could have written the entire operation as:
Ps = A*points - b;
Perhaps the author chose to expand those expressions for some unknown reason, or perhaps the author was a bit of a novice themselves to programming in MATLAB. But we just don't know what they were thinking.
Next, good code includes comments. The comments make the code readable. They make it usable to someone else in the world. And no, I don't consider some code as good I received one day, where the only comment line in the entire 300 lines of code was
"create C matrix here"
And then they went ahead, and created a matrix called C.
Again, good code includes explanatory comments. It should be positively verbose. So no, I won't describe that as good code to use as a learning tool.
Now, can I possibly make any sense of what I see there? It LOOKs as if what we see is a set of linear equations, that describe what may be effectively inequalities. If we interpret each row of A as the equation of one line, combined with the corresponding element of b, then the expression I coded more simply as:
Ps = A*points - b
just tells you which side of each line the point falls on. If the result is positive, then it lies on one side of the line. A negative result tells you the point lies on the other side. As such, the first column of Ps tells us the first point lies above all three lines.
The other 4 points lie "above" some lines, and "below" others, with the only exception that point #4 lies exactly on one of the lines. The zero tells you that.
Is such a computation of any value? Well, it might be used in terms of an optimization routine, to define a set of linear inequalities. So something like fmincon or lsqlin or linprog might use something similar. Another circumstance where we might see such a computation is to learn if a point lies inside a triangle, or a simplex in higher dimensions. Lacking any context or comments, it is impossible to know what was intended.
The best, and possibly only person to try to make true sense of it would be the author, because code, uncommented and out of context is just random meaningless junk. My recommendation would be to find a better source to learn from.
댓글 수: 0
추가 답변 (1개)
Govind KM
2024년 12월 2일
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!
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!