How to reorganize an array given an index

3 ビュー (過去 30 日間)
Scarlet Passer
Scarlet Passer 2020 年 11 月 25 日
コメント済み: Scarlet Passer 2020 年 11 月 25 日
So I have a 65x1 array in which the first 42 elements are numbers and the last 23 elements are NaN. I would like to reorganize the array by putting the NaNs at the end into certain indicies within the array that I have flagged.
More simplistically I want something like this:
array = [1,2,3,4,5,NaN,NaN,NaN];
idx_flag = [1,4,8];
%i want
new_array = [NaN,1,2,NaN,3,4,5,NaN];
Thanks!

採用された回答

the cyclist
the cyclist 2020 年 11 月 25 日
編集済み: the cyclist 2020 年 11 月 25 日
Here is one way:
% Inputs
array = [1,2,3,4,5,NaN,NaN,NaN];
idx_flag = [1,4,8];
% Get length of array for convenience
L = numel(array);
% Find where the non-NaNs will go
not_idx_flag = setxor(1:L,idx_flag);
% Allocate a new array with all NaNs
new_array = nan(1,L);
% Place the non-NaNs
new_array(not_idx_flag) = array(~isnan(array));
  1 件のコメント
Scarlet Passer
Scarlet Passer 2020 年 11 月 25 日
Thanks so much it worked great!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBartlett についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by