How to add extra columns at specified locations

1 view (last 30 days)
Hi!
I have a variable called "dataset" that corresponds to a vector containing numbers and nan values. Let say :
dataset = [1, 2, 3, nan, 4, 5, nan, 6, 7]
For the rest of the computation, i need to create a second vector, "dataset2", that lacks the nan values. Let say:
dataset2 = [1, 2, 3, nan, 4, 5, nan, 6, 7]
Some computation is done on dataset2. Let say "dataset2" is transformed into:
dataset2 = [2, 5, 7, 8, 4, 8, 10]. Now, I need to reconstruct a vector that is the same size as the "dataset" vector, and that contains the values of dataset2 after computation and also contains nan at their original location as:
dataset3 = [2, 5, 7, nan, 8, 4, nan, 8, 10]
I'm stuck to perform the last step. Any help would be appreciated.
Best,
Guillaume

Accepted Answer

Bruno Luong
Bruno Luong on 1 Feb 2024
Edited: Bruno Luong on 1 Feb 2024
After computing dataset2
dataset = [1, 2, 3, nan, 4, 5, nan, 6, 7];
% ...
dataset2 = [2, 5, 7, 8, 4, 8, 10];
do this:
dataset3 = dataset;
dataset3(~isnan(dataset3)) = dataset2
dataset3 = 1×9
2 5 7 NaN 8 4 NaN 8 10

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!