How to fill differences between two vectors with NaN to make equal lengths?

240 Views Asked by At

I am searching for a quick way to compare two vectors of different lengths and fill the shorter array's missing places with NaN values.

Example:

a = [2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2102 2103 2104 2105 2106 2108 2109 2110 2111]

b = [2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2106]

I want to compare b to a and everywhere where there is a difference, I need a NaN value, so that in the end they have the same length:

c = [NaN 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 NaN  NaN NaN NaN 2106 NaN NaN NaN NaN]

I figured I can use setdiff to find out the differences between a and b: [s,idx] = setdiff(a,b)

But I can't figure out what would be the best way to insert the NaN values into b, because if do e.g. b(idx(1)) == NaN then I overwrite what was in b in the first element. Can somebody point me please to the right direction?

1

There are 1 best solutions below

1
On BEST ANSWER

My assumption is, that b is a subset of a. If there are elements in b that are not in a, please provide a logic, how to handle this case.

MATLAB's ismember function is your friend here (tested with Octave 5.2.0 and MATLAB Online):

close all;
close all;
clc;

a = [2090, 2091, 2092, 2093, 2094, ...
     2095, 2096, 2097, 2098, 2099, ...
     2100, 2102, 2103, 2104, 2105, ...
     2106, 2108, 2109, 2110, 2111]

b = [2091, 2092, 2093, 2094, 2095, ...
     2096, 2097, 2098, 2099, 2100, ...
     2106]

% Get indices, where to find values from b in a     
idx = ismember(a, b)

% Create nan vector with the size of a
c = nan(size(a));

% Get correct values from the found indices
c(idx) = a(idx)

Output:

a =
   2090   2091   2092   2093   2094   2095   2096   2097   2098   2099   2100   2102   2103   2104   2105   2106   2108   2109   2110   2111

b =
   2091   2092   2093   2094   2095   2096   2097   2098   2099   2100   2106

c =
    NaN   2091   2092   2093   2094   2095   2096   2097   2098   2099   2100    NaN    NaN    NaN    NaN   2106    NaN    NaN    NaN    NaN