Hello everybody,
I would like to find the "intersections" of different data arrays. The arrays look similar this:
x=[0 1.2 2.5 3.4 4.6 5.4 6.8 7.3 8.6 9.8 7.1 8.5 9.9 10.1 11.4]
y=[100 90 80 70 65 60 55 50 45 40 40 42 44 52 62]
I would like to cut off the part under the intersection and redefine the data set. So it would be enough to find the two connection points (in this example (8.6;45) and (9.9;44)). Any ideas, how to do this? I have ran out of ideas and really need new ideas/inspiration from you guys!
Cheers, Christian

댓글 수: 6

Not sure what you mean. I don't see how there is any intersection going on at 8.6 and 9.9.
x=[0 1.2 2.5 3.4 4.6 5.4 6.8 7.3 8.6 9.8 7.1 8.5 9.9 10.1 11.4]
y=[100 90 80 70 65 60 55 50 45 40 40 42 44 52 62]
plot(x, y, 'bo-', 'LineWidth', 3)
grid on;
xticks(0:0.5:12)
hold on;
for k = 1 : length(x)
str = sprintf(' %d', k);
text(x(k), y(k), str, 'FontSize', 25, 'FontWeight', 'bold', 'Color', 'r');
end
xlabel('X', 'FontSize', 25);
ylabel('Y', 'FontSize', 25);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
What does "cut off the part under the intersection" really mean? In the plot above, which points do you want to delete or do something else with???
Hello Image Analyst, sorry for my imprecise question. I would like to delete points 10, 11 and 12 so that there would be a line between 9 and 13. So the data set would look like this:
x_new=[0 1.2 2.5 3.4 4.6 5.4 6.8 7.3 8.6 9.9 10.1 11.4]
y_new=[100 90 80 70 65 60 55 50 45 44 52 62]
I have to do this for a lot of different arrays which all look pretty similar to the given example. Therefore I would like to write a piece of code which does that automatically so that I don't have to look for (e.g.) Points 9 and 13 manually in the plot every time. Thank you for your effort, Cheers Christian!
Selby
Selby 2018년 9월 3일
you could possibly write a script that removes any points with a lesser x value than the last. However that would mean that you would have 9->10->13
Christian
Christian 2018년 9월 4일
@Selby,
That's what I already did. But as you have mentioned, it's not 100% the solution I'm looking for.
It's pretty weird, because you can find intersections of functions since middle school, but I'm really struggeling right now, to do something similar on a data-sets.
What would be the expected result for a case such as
9
13
10
11 12
where the data "doubles back" but does not intersect itself?
Christian
Christian 2018년 9월 4일
@Walter Roberson
all of my data-sets intersect, so I won't face a case as you have described. But thanks to "Stephen Cobeldick" I found a perfect solution for my Problem -> check the answer below!

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

 채택된 답변

Stephen23
Stephen23 2018년 9월 4일
편집: Stephen23 2018년 9월 4일

0 개 추천

Download and use selfintersect:
I tried it on your example values:
>> x = [0,1.2,2.5,3.4,4.6,5.4,6.8,7.3,8.6,9.8,7.1,8.5,9.9,10.1,11.4];
>> y = [100,90,80,70,65,60,55,50,45,40,40,42,44,52,62];
>> [~,~,s] = selfintersect(x,y)
s =
9 12
>> xnew = x([1:s(1),1+s(2):end])
xnew =
0.00000 1.20000 2.50000 3.40000 4.60000 5.40000 6.80000 7.30000 8.60000 9.90000 10.10000 11.40000
>> ynew = y([1:s(1),1+s(2):end])
ynew =
100 90 80 70 65 60 55 50 45 44 52 62
>> plot(x,y,'-*',xnew,ynew,'-*')
If you have multiple intersects you will have to use a loop (or some alternative) to process each row of the output matrix s.

댓글 수: 3

Christian
Christian 2018년 9월 4일
This function is absolutely perfect! It does exactely what I was looking for! Thank you Stephen :)
Stephen23
Stephen23 2018년 9월 4일
@Christian: I hope that it helps. If you find selfintersects useful, then please also go and give it a rating at the page where you downloaded it from. I am sure that its author will appreciate that.
Christian
Christian 2018년 9월 5일
Done! :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 9월 3일

댓글:

2018년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by