Finding corresponding values in data set

1 Ansicht (letzte 30 Tage)
jrz
jrz am 16 Sep. 2022
Kommentiert: Star Strider am 16 Sep. 2022
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 Kommentare
Walter Roberson
Walter Roberson am 16 Sep. 2022
Is there always exactly one 0 or zero crossing, or could there be several?
are the values in that column sorted?
jrz
jrz am 16 Sep. 2022
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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 16 Sep. 2022
Bearbeitet: Star Strider am 16 Sep. 2022
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 Kommentare
jrz
jrz am 16 Sep. 2022
so sorry for the confusion!, i misspoke in my reply. Thanks again for your help, i understand now
Star Strider
Star Strider am 16 Sep. 2022
As always, my pleasure!
No worries!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by