Finding corresponding values in data set

I have a set of matrices each with 4 columns. I want to extract the value of the 1st column corresponding to the 0 in the second column and plot that point. How can I do this? and for cases where there is no exact zero, interpolate between the two values that cross 0?

댓글 수: 2

Is there always exactly one 0 or zero crossing, or could there be several?
are the values in that column sorted?
jrz
jrz 2022년 9월 16일
yes there is only a single zero crossing or single 0 for each. The values in the columns are in ascending order, e.g. -5 at (1,) and 2 at (1,50)

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

 채택된 답변

Star Strider
Star Strider 2022년 9월 16일
편집: Star Strider 2022년 9월 16일
I would just do the interpolation using interp1 since it will interpolate to 0 or the closest value to it.
Try this —
M = randn(10,4)
M = 10×4
-0.9697 -0.7712 -1.3959 1.3028 -1.7855 -0.0497 0.0510 -0.6687 -0.3479 0.4030 -0.8218 0.5535 0.6310 1.3745 -2.0030 -0.6301 -0.3321 -0.3106 0.8438 -0.4688 -2.2774 -0.0997 0.3758 -2.0011 -0.0985 0.6001 0.4742 0.0693 -1.8270 1.2099 0.1478 -0.5050 0.4731 -0.3345 1.4999 0.3136 -0.4747 -0.7732 0.6202 0.6346
L = size(M,1);
idx = find(diff(sign(M(:,2))))
idx = 4×1
2 4 6 8
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(L,idx(k)+1);
Result(k,:) = interp1(M(idxrng,2), M(idxrng,:),0);
end
Result
Result = 4×4
-1.6276 -0.0000 -0.0449 -0.5344 -0.3390 -0.0000 0.1189 -0.0239 -1.9668 0 0.3898 -1.7060 0.2685 -0.0000 1.1328 0.2262
EDIT — Aesthetic tweaks.
.

댓글 수: 4

jrz
jrz 2022년 9월 16일
Thanks for your reply, but i dont really undertand whats going on here. For your M i would just want a single value from the second column that corresponds to where the values in the 1st column cross zero. is that what the result matrix is saying in some form?
My pleasure!
I want to extract the value of the 1st column corresponding to the 0 in the second column and plot that point.’
I am slightly confused now, since this is different from the original question.
This retrieves the values for the first two columns when the second (or first) column crosses (or equals) zero —
M = randn(10,4)+0.7
M = 10×4
-0.2369 1.8739 0.0779 0.7713 0.6648 0.8674 1.3288 0.6227 2.6427 -0.9644 -0.0458 1.4343 -0.7491 0.3339 1.1961 0.5221 1.7124 0.8753 2.4023 1.4774 0.0145 2.6902 1.0609 0.9595 1.0549 1.1404 1.3261 2.5583 -1.0573 1.9985 2.2614 -2.3209 0.5639 -0.0326 -0.1718 -0.6114 0.9802 0.0070 0.6125 -0.5356
L = size(M,1);
idx = find(diff(sign(M(:,2))))
idx = 4×1
2 3 8 9
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(L,idx(k)+1);
Result2(k,:) = interp1(M(idxrng,2), M(idxrng,1:2),0);
end
Result2 % First & Second Columns When Second Column Crosses Zero
Result2 = 4×2
1.6014 -0.0000 0.1233 -0.0000 0.5775 0 0.9062 0.0000
idx = find(diff(sign(M(:,1))))
idx = 5×1
1 3 4 7 8
for k = 1:numel(idx)
idxrng = max(1,idx(k)-1) : min(L,idx(k)+1);
Result1(k,:) = interp1(M(idxrng,1), M(idxrng,1:2),0);
end
Result1 % First & Second Columns When First Column Crosses Zero
Result1 = 5×2
0.0000 1.6094 -0.0000 0.6166 -0.0000 0.4987 0.0000 2.6808 0 0.6739
I do not have your data, so I am not certain how you want the points plotted.
Other interpolation methods (for example 'nearest') are possible if you do not want a linear (default) interpolation, and only the nearest point to the zero-crossing.
.
jrz
jrz 2022년 9월 16일
so sorry for the confusion!, i misspoke in my reply. Thanks again for your help, i understand now
As always, my pleasure!
No worries!

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

추가 답변 (0개)

카테고리

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

태그

질문:

jrz
2022년 9월 16일

댓글:

2022년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by