How can I join unequal vectors by adding NaN values?
10 views (last 30 days)
Show older comments
Benjamin Horsley
on 25 Feb 2021
Commented: Steven Lord
on 26 Feb 2021
I have two column vectors of unequal length (basic example below). The first is a vector of known events (target), whereas the second is a vector of predictions (prediction) of those events. The prediction essentially detects three more events than the target, hence the unequal lengths of the two vectors.
target = [2 3 4 5 6 7 8 9 10]';
prediction = [1 2 3 4 5 6 7 8 9 10 11 12]';
Basically, I want to merge these two together to form a double array/table by adding elements (NaN) at the start and end of the target vector to match the length of prediction. Is there a way to add n number of NaN values to the start and end of target, depending on how many more prediction has before and after the smallest and largest values of target? For example, if prediction has 1 element less than the smallest value (2) of target, I want to add 1 NaN to the start of target. Conversely, if prediction has 2 elements greater than the largest value (10) of target, I want to add 2 NaN to the end of target, and so on. The example below is what I would like to get to. However, the number of predictions relative to targets may not always be the same.
result =
NaN 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
NaN 11
NaN 12
I then hope to convert numeric values to 1 and NaN to 0, so I get:
logical =
0 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
0 1
0 1
In short, I aim to perform some form of sensitivity/specificity analysis in another program to test the prediciton versus the target. If anyone can please help me go about achieving this, it will be greatly appreciated. Certainly open to suggestions!
Thank you.
0 Comments
Accepted Answer
Steven Lord
on 25 Feb 2021
The NaN function (along with inf, zeros, and ones) behaves in a way that will help you. Negative sizes are treated as 0.
a = NaN(1, 2)
b = NaN(1, -2)
c = [a, 1:3]
d = [1:5, b]
2 Comments
Steven Lord
on 26 Feb 2021
Why did I use 2 and -2 in constructing a and b? Are those two lengths related to what's in c and d? [The answer to the latter question is yes.]
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!