[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Filling the gaps in data arrays
From: |
Przemek Klosowski |
Subject: |
Re: Filling the gaps in data arrays |
Date: |
Thu, 26 Jul 2018 11:37:15 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 |
On 07/26/2018 10:47 AM, Mat086 wrote:
I don't need to interpolate the data, actually the best thing would be to
have NaN where the y-data doesn't match the X vector.
this script is doing what I needed to do:
dx = x(2)-x(1);
Y = zeros(1,length([min(x):dx:max(x)]));
X = [min(x):dx:max(x)];
st=1;
for k=1:length(X)
if isempty(y(x==X(k)))
Y(k) = 0;
else
Y(k) = y(st);
st=st+1;
end
end
You still have the problem with accuracy; I think you will zero out some
valid numbers because your calculated X values do not have to fall
exactly on your x values.
For a contrived example, x=[0 0.3333333 0.666667 1]
x =
0.00000 0.33333 0.66667 1.00000
octave:14> dx = x(2)-x(1);
octave:15> Y = zeros(1,length([min(x):dx:max(x)]));
octave:16> X = [min(x):dx:max(x)]
X =
0.00000 0.33333 0.66667 1.00000
octave:17> x==X
ans =
1 1 0 0
As you see, x and X are so very close that they print as identical, but
they do not match. You can search for closeness:
my_eps=1e-6;
match = ( abs(x-X)<my_eps )
and then exploit the match vector to just transfer the matching y values:
dx = x(2)-x(1);
X = [min(x):dx:max(x)];
Y = zeros(1,length(X));
Y(find(match))= y
Octave/Matlab language has those 'vector' expressions that make it quite fast
and concise. The cost is maybe the extra storage (e.g. the match vector, and
the temporary vectors used by the find() operation) but in practice it is a big
win and it's good to learn to write code this way.
BTW, if you want to really use NaNs, you could do this:
Y = repmat(NaN,1,length(X));