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?
My assumption is, that
b
is a subset ofa
. If there are elements inb
that are not ina
, 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):Output: