This is a fairly simple task I want to perform, but I can’t seem to figure out a way to do it. I’ve tried sortrows, reshaping, and other solutions, but none of them do exactly what I want.
Essentially, I have two vectors from the same range of values, of unequal lengths. Some of the values are equal, some are not. E.g.
A = [1 5 20 30 53 70 92]
B = [2 3 4 16 20 30 60 95 100]
What I want to do is add “NaNs” to each vector to “stand in” for the values in the other vector that aren’t shared. So, I want them to look like:
A = [1 NaN NaN NaN 5 NaN 20 30 53 NaN 70 92 NaN NaN]
B = [NaN 2 3 4 NaN 16 20 30 NaN 60 NaN NaN 95 100]
Some method by which the vector will have placeholders for the value of the other vector.
Do I combine the vectors, sort it, then somehow search and replace all values from the other vector with NaNs? That seems like a bit of a clunky solution, though not impossible. I feel like there is some more elegant way to accomplish this that I am missing.
Thanks!
Here is one solution using a simple map:
Comparison with Rodys solution shows this method is a bit faster: