Suppose I have a vector, say S=1:10, and a fixed number K=5. Is there a way to vectorize the following code?
v=zeros(1:10);
for i=1:10
if S(i)>K
v(i)=S(i)-K;
else
v(i)=0;
end
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just for educational purposes:
Note that this gives you some flexibility, you can easily replace the final
0byNaNor even remove all these entries by replacing it with[]