Creating a variable with if statement

Hi... I'm struggling with this for hours...
Suppose there is a column vector X:
1
5
7
8
9
4
2
.
.
.
and I would like to create another vector, Y, such that X < 6 but keep the order:
1
5
missing
missing
missing
missing
4
2
.
.
.
Please help. Thank you!

 採用された回答

madhan ravi
madhan ravi 2020 年 6 月 3 日

1 投票

y = num2cell(x);
y(x>6) = {'missing'}

1 件のコメント

JB
JB 2020 年 6 月 4 日
Thank you so much!!!

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

その他の回答 (1 件)

jmac
jmac 2020 年 6 月 3 日
編集済み: jmac 2020 年 6 月 3 日

0 投票

Just eliminate the entries you don't want: y=x; y(y>=6)=[];
Or, if you want to maintain the size and class (vector), NaN: y=x; y(y>=6)=NaN;

4 件のコメント

madhan ravi
madhan ravi 2020 年 6 月 3 日
Nope not true , you remove the elements. There’s a difference between missing and removing.
jmac
jmac 2020 年 6 月 3 日
Just providing a JB with a couple more options.
madhan ravi
madhan ravi 2020 年 6 月 3 日
編集済み: madhan ravi 2020 年 6 月 3 日
Joao not a problem;), didn’t mean it in a rude way. Thought it was OP but then realised it’s a different person, sorry! And Ofcourse you’re edited answer is the way to go.
JB
JB 2020 年 6 月 4 日
Thank you as well!

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

JB
2020 年 6 月 3 日

コメント済み:

JB
2020 年 6 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by