Help!!!!!!!! How to replace all the negative values in an array with zeros?

29 ビュー (過去 30 日間)
Rooy
Rooy 2012 年 3 月 12 日
Would anyone know how to use the vectorization method for this question? I did it the loop method but I would also need to do it for vectorization method, any ideas?
y(t) =sin(t) for all t where sint>0
elsewhere
y(t)=0
for -6 π ≤ t ≤ 6 π at intervals of π/10. Use vectorization to solve it.
This is how I did it with loops
%Inilization
aa=1;
ii=0;
t=0;
%Running the function for each -6pi to 6pi with an interval of pi/10
for t=input('input vector t:')
if sin(t)>0
y(aa)=sin(t);
else
y(aa)=0;
end
aa=aa+1;
ii=ii+1;
end
% Displaying the value for each count
t=-6*pi:pi/10:6*pi;
a=1:ii;
table=[a' y' t'];
How would I change the negative values in vector y to zero using vectorization in a way?
Thank you

採用された回答

Honglei Chen
Honglei Chen 2012 年 3 月 12 日
Did you delete your original question? I answered this several days ago. Please do not delete your question. Instead, if the answer is not what you want, update your question or add comments.
For this question, you can use logical index
t = (-6:0.1:6)*pi;
y = sin(t);
y(y<0) = 0;
  1 件のコメント
Rooy
Rooy 2012 年 3 月 12 日
Thank you for all the help and sorry I deleted the old question. Thanks

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 3 月 12 日
y = max(0, y);
  1 件のコメント
Rooy
Rooy 2012 年 3 月 12 日
Thank you, this work too, learnt a lot.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by