Skipping certain values in an array but still continuing the array

I have written a code to skip negative values, but it gives an error, i only want positive values of an array while the negative values must be skipped. (I also dont want zeros in place of these negative values, i just want an array without the negative values)
The code is,
y = zeros(100,1);
for i = 1:100
j = i/10;
y(i,1) = sin(j);
if y(i,1)<0
y(i,1) = [];
end
end
But this gives error
I also tried using the 'break' command, but it gives all zeros just after the first negative value has been encountered.
Can you please suggest a way out?

 採用された回答

madhan ravi
madhan ravi 2020 年 6 月 28 日

0 投票

y = nonzeros(sin(1:1e2));

5 件のコメント

Sandip Ghatge
Sandip Ghatge 2020 年 6 月 28 日
Thank you for your answer @madhan ravi,
but that just removes the zero values of an array and not the negative values.
I am looking for removing all the negative values
madhan ravi
madhan ravi 2020 年 6 月 28 日
y =(sin(1:1e2)<0) .* sin(1:1e2)
Sandip Ghatge
Sandip Ghatge 2020 年 6 月 28 日
I think there is a slight typing mistake it must be, (">" in place of "<")
y =(sin(1:1e2)>0) .* sin(1:1e2);
but i am sorry to say that it still does not skip the negative values, but it just replaces the negative values with zeros, and includes these zeros in the array.
Something like this mentioned in an image,
What i am looking for is an array with
0.8415, 0.9093, 0.1411, 0.6570, 0.9894, 0.4121, 0.4202, 0.9906
madhan ravi
madhan ravi 2020 年 6 月 28 日
y = nonzeros((sin(1:1e2)>0) .* sin(1:1e2));
Sandip Ghatge
Sandip Ghatge 2020 年 6 月 28 日
Thank you, that gives the desired answer.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by