How to plot the vector relative black point to red point

2 次查看(过去 30 天)
Hello, I am a Matlab user beginner. I would like to seek your help about how to get the vector relative black point and red point. (like a black arrow). Thank you very much. The red position is red(x,y) =[25 257
64 181
64 334
68 31
105 257
106 104
107 410
147 181
147 334
148 28
149 487
189 104
189 257
189 410
229 181
230 28
230 333
230 486
269 104
271 410
272 257
313 28
313 181
313 334
313 486
354 104
355 257
355 410
396 180
396 334
397 28
397 486
438 104]
black postion(x,y) = [ 63 332
71 184
102 403
112 256
119 111
152 327
160 182
163 35
192 398
201 252
205 109
235 471
244 323
246 181
251 37
283 395
288 250
289 105
325 467
328 318
330 179
331 35
369 249
371 105
372 393
414 176
415 320
415 466
454 103
455 392
456 248
492 176
493 320]
  1 个评论
VBBV
VBBV 2020-12-30
Take the distance between the two points on red and black vectors. Then Use quiver function to plot them

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-12-30
编辑:Adam Danz 2021-1-5
  1. Your [x,y] coordinates are not paired so step 1 is to find the values in black ('k') that are closest to red ('r') (or vise versa).
  2. Some coordinates do not have close pairs. To detect and ignore those coordinates this solution thresholds the paired distances to 2 times the standard deviation of all distances and eliminates any pairs that exceed that distance from the mean distance.
  3. This solution uses line() to plot lines between the final list of paired (x,y) coordinates.
% r and k are nx2 matrices of [x,y] coordinates
r =[25 64 64 68 105 106 107 147 147 148 149 189 189 189 229 230 230 230 269 271 272 313 313 313 313 354 355 355 396 396 397 397 438;
257 181 334 31 257 104 410 181 334 28 487 104 257 410 181 28 333 486 104 410 257 28 181 334 486 104 257 410 180 334 28 486 104]';
k = [63 71 102 112 119 152 160 163 192 201 205 235 244 246 251 283 288 289 325 328 330 331 369 371 372 414 415 415 454 455 456 492 493;
332 184 403 256 111 327 182 35 398 252 109 471 323 181 37 395 250 105 467 318 179 35 249 105 393 176 320 466 103 392 248 176 320]';
% Resort k according to proximity to r
dist = pdist2(r,k);
[mindist, minIdx] = min(dist);
% Eliminate connects that are greater than 2 std from mean distance
% This works for these data but may need tweeked for other data
eliminateIdx = mindist > mean(mindist) + std(mindist)*2;
% Store paired coordinates
rPair = r(minIdx(~eliminateIdx),:);
kpair = k(~eliminateIdx,:);
% Plot original data
clf()
hold on
scatter(r(:,1),r(:,2),50,'r')
scatter(k(:,1),k(:,2),50,'k')
axis equal
grid on
% Add connection lines
line([kpair(:,1),rPair(:,1)]', [kpair(:,2),rPair(:,2)]', 'Color', 'b')
Alternatively, you could use quiver as VBBV mentioned but it's important to set the scaling factor to 0 since you're working with exact end points of the vectors.
xDist = rPair(:,1) - kpair(:,1);
yDist = rPair(:,2) - kpair(:,2);
% Plot original data
figure()
hold on
scatter(r(:,1),r(:,2),50,'r')
scatter(k(:,1),k(:,2),50,'k')
axis equal
grid on
% Add quiver arrows
quiver(kpair(:,1),kpair(:,2), xDist, yDist, 0, 'Color', 'b')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Vector Fields 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by