As you can see, the step in x array is fixed to x(2)-x(1)=1 (in this
example), but there are gaps in both x and y in regions where background is
supposed to be flat.
However, In order to analyze such kind of data with another software I need
to create X and Y vectors without holes. Therefore, I should create an X
array X=[1:step:64], of length=64, and Y array of the same length, having
the peaks at the same positions of [x y] and zeros where there were gaps in
x. There was an in built function in Matlab ("fillmissing" if I remember
well), but I'm struggling to get this done in Octave.
Any help would be greatly appreciated!

As you can see, the step in x array is fixed to x(2)-x(1)=1 (in this
example), but there are gaps in both x and y in regions where background is
supposed to be flat.
However, In order to analyze such kind of data with another software I need
to create X and Y vectors without holes. Therefore, I should create an X
array X=[1:step:64], of length=64, and Y array of the same length, having
the peaks at the same positions of [x y] and zeros where there were gaps in
x. There was an in built function in Matlab ("fillmissing" if I remember
well), but I'm struggling to get this done in Octave.
Any help would be greatly appreciated!

On the best of my knowledge, this should be working fine in Matlab, but with
octave I get the following error as soon as k=3:
......
index = 1
index = 2
index = [](1x0)
error: test_00: =: nonconformant arguments (op1 is 1x1, op2 is 1x0)
......

And no, it won't work in Matlab either because you are doing exact
floating point comparisons on calculated values, so any roundoff will
trip you up---check for yourself, X==X(3) returns matches, but X==x(3)
probably doesn't.

You said that "In order to analyze such kind of data with another
software I need to create X and Y vectors without holes". What is
exactly the assumption there? Is the original dataset sampled on a
regular grid, and the non-zero values are dropped?

It seems to me that the best solution for you would be to interpolate
your data onto a regular grid, perhaps like this:

Re: Filling the gaps in data arrays

Przemek Klosowski-7 wrote

> On 07/12/2018 04:36 AM, Mat086 wrote:
>> dx = x(2)-x(1);
>> Y = zeros(1,length([min(x):dx:max(x)]));
>> X = [min(x):dx:max(x)];
>> for k=1:length(x)
>> indx = find(X==x(k));
>> Y(k) = y(index);
>> endfor
>> plot(x,y,'b-'); hold on; plot(X,Y,'-o');
>
> you mean Y(k) = y(indx); , right?
>
> And no, it won't work in Matlab either because you are doing exact
> floating point comparisons on calculated values, so any roundoff will
> trip you up---check for yourself, X==X(3) returns matches, but X==x(3)
> probably doesn't.
>
> You said that "In order to analyze such kind of data with another
> software I need to create X and Y vectors without holes". What is
> exactly the assumption there? Is the original dataset sampled on a
> regular grid, and the non-zero values are dropped?
>
> It seems to me that the best solution for you would be to interpolate
> your data onto a regular grid, perhaps like this:
>
> dx=x(2)-x(1);
>
> plot(x,y,'x',X=min(x):dx:max(x),interp1(x,y,X))

Hi Przemek,
thanks for your reply!
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:

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 =

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));

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 =

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));
&lt;/quote>