How can I join unequal vectors by adding NaN values?

10 次查看(过去 30 天)
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.

采纳的回答

Steven Lord
Steven Lord 2021-2-25
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)
a = 1×2
NaN NaN
b = NaN(1, -2)
b = 1×0 empty double row vector
c = [a, 1:3]
c = 1×5
NaN NaN 1 2 3
d = [1:5, b]
d = 1×5
1 2 3 4 5
  2 个评论
Benjamin Horsley
Benjamin Horsley 2021-2-26
Thanks, @Steven Lord. Could you please expand on how I would automate this to include any number of NaN values at the start and end of the shortest vector (target) which are dependent on how many elements prediction has that a smaller and larger than target(1) and target(end)? Appreciate any further feedback.
Steven Lord
Steven Lord 2021-2-26
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.]

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by