How to assign NaN to consecutive elements of a vector, in intervals defined by a matrix

1 回表示 (過去 30 日間)
Hello there!
supose we have a vector x=1:12
and we have a controling matrix as this:
z=[2 4; 6 8; 10 11]
We want to assign NaN in x in the intervals in z for 2 to 4, 6 to 8 and 10 to 11, in order to get:
x_new= [ 1 NaN NaN NaN 5 NaN NaN NaN 9 NaN NaN 12]
Many thanks in advance for your time
  2 件のコメント
Mohammad Sami
Mohammad Sami 2020 年 6 月 5 日
did you mistakenly type the wrong intervals for x ?
Paramonte
Paramonte 2020 年 6 月 5 日
Dear Mohammad, you are right. I have edited the original question and corrected the values.
Thank you

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

採用された回答

Tommy
Tommy 2020 年 6 月 5 日
Try this:
N = numel(x);
idx = any((1:N >= z(:,1) & 1:N <= z(:,2)));
x(idx) = NaN;
  2 件のコメント
Paramonte
Paramonte 2020 年 6 月 7 日
Tommy: works perfectely, thank you for your time and effort!
Tommy
Tommy 2020 年 6 月 7 日
Happy to help!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by