How to add extra columns at specified locations

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

采纳的回答

Bruno Luong
Bruno Luong 2024-2-1
编辑:Bruno Luong 2024-2-1
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by