필터 지우기
필터 지우기

I really need help with fprintf'ing a string, having it alternate with commas and parentheses.

조회 수: 3 (최근 30일)
"I really need help with fprintf'ing a string, having it alternate with commas and parentheses."
How do I write a function to fprintf the following string, so that it also changes when the input is changed!:
"convex points: 6 (1 15), 7 (2 6), 8 (6 1), 10 (16 7), 12 (19 18) and 5 (9 19)."
aka
"convex points: firstConvexPoint (coordsX coordsY), secondConvexPoint (coordsX coordsY)"
the values are already sorted properly in arrays, but I can't figure out how to code the string that it alternates in such a way that I get the string above..
Any help is hugely appreciated.
Code:
clear;
close all;
clc;
xy = [
3 12 % Point 1
10 8 % Point 2
11 14 % Point 3
13 16 % Point 4
9 19 % Point 5
1 15 % Point 6
2 6 % Point 7
6 1 % Point 8
12 5 % Point 9
16 7 % Point 10
14 17 % Point 11
19 18 % Point 12
];
xy = xy';
convexHull = getConvexHull(xy);
coords = xy(:,convexHull)';
coordsX = coords(:,1);
coordsY = coords(:,2);
fprintf
function k = getConvexHull(xy)
[m,n] = size(xy);
if m~=2
error('convexhull: xy must have 2 columns');
end
[xmin,first] = min( xy(1,:) );
ind = [1:(first-1) (first+1):n];
angle = atan2( xy(1,ind)-xy(1,first), xy(2,ind)-xy(2,first) );
[junk,order] = sort(angle);
ind = [ind(order) first];
stack = zeros( n, 1, 'uint32' );
stack(1) = first;
stacktop = 1;
i = 1;
while i<=n
if stacktop<2
stacktop = stacktop+1;
stack(stacktop) = ind(i);
i = i+1;
else
p0 = xy(:,stack(stacktop));
p1 = xy(:,stack(stacktop-1));
p2 = xy(:,ind(i));
if (p1(1)-p0(1))*(p2(2)-p0(2))-(p2(1)-p0(1))*(p1(2)-p0(2)) >= 0
stacktop = stacktop+1;
stack(stacktop) = ind(i);
i = i+1;
else
stacktop = stacktop-1;
end
end
end
k = stack(1:stacktop);
k = flip(k);
k(end) = [];
end
  댓글 수: 3
Stephen23
Stephen23 2019년 12월 15일
Original question (in case this gets deleted for a third time):
"I really need help with fprintf'ing a string, having it alternate with commas and parentheses."
How do I write a function to fprintf the following string, so that it also changes when the input is changed!:
"convex points: 6 (1 15), 7 (2 6), 8 (6 1), 10 (16 7), 12 (19 18) and 5 (9 19)."
aka
"convex points: firstConvexPoint (coordsX coordsY), secondConvexPoint (coordsX coordsY)"
the values are already sorted properly in arrays, but I can't figure out how to code the string that it alternates in such a way that I get the string above..
Any help is hugely appreciated.
Code:
clear;
close all;
clc;
xy = [
3 12 % Point 1
10 8 % Point 2
11 14 % Point 3
13 16 % Point 4
9 19 % Point 5
1 15 % Point 6
2 6 % Point 7
6 1 % Point 8
12 5 % Point 9
16 7 % Point 10
14 17 % Point 11
19 18 % Point 12
];
xy = xy';
convexHull = getConvexHull(xy);
coords = xy(:,convexHull)';
coordsX = coords(:,1);
coordsY = coords(:,2);
fprintf
function k = getConvexHull(xy)
[m,n] = size(xy);
if m~=2
error('convexhull: xy must have 2 columns');
end
[xmin,first] = min( xy(1,:) );
ind = [1:(first-1) (first+1):n];
angle = atan2( xy(1,ind)-xy(1,first), xy(2,ind)-xy(2,first) );
[junk,order] = sort(angle);
ind = [ind(order) first];
stack = zeros( n, 1, 'uint32' );
stack(1) = first;
stacktop = 1;
i = 1;
while i<=n
if stacktop<2
stacktop = stacktop+1;
stack(stacktop) = ind(i);
i = i+1;
else
p0 = xy(:,stack(stacktop));
p1 = xy(:,stack(stacktop-1));
p2 = xy(:,ind(i));
if (p1(1)-p0(1))*(p2(2)-p0(2))-(p2(1)-p0(1))*(p1(2)-p0(2)) >= 0
stacktop = stacktop+1;
stack(stacktop) = ind(i);
i = i+1;
else
stacktop = stacktop-1;
end
end
end
k = stack(1:stacktop);
k = flip(k);
k(end) = [];
end

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 12월 4일
편집: Stephen23 2019년 12월 4일
[~,idx] = ismember(coords,xy.','rows'); % Better: obtain these indices from your function!
mat = [idx,coords].'; % should have size 3xN for N points.
fprintf('convex points:'); % prints once.
fprintf(' %d (%d %d),',mat(:,1:end-1)) % prints N-1 times.
fprintf(' and %d (%d %d).\n',mat(:,end)) % prints once.
Which prints:
convex points: 6 (1 15), 7 (2 6), 8 (6 1), 10 (16 7), 12 (19 18), and 5 (9 19).
Tip: avoid confusion be not transposing xy all the time: pick one orientation and stick to it.
  댓글 수: 1
The Merchant
The Merchant 2019년 12월 4일
Thank you so much!
Is there maybe an easy-to-do edit so that it says ...18) and 5 (9... instead of ...18), and 5 (9...?
Anyways, very big thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by