How to add extra columns at specified locations

1 回表示 (過去 30 日間)
GMabilleau
GMabilleau 2024 年 2 月 1 日
編集済み: Bruno Luong 2024 年 2 月 1 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by