How can i select certain elements from a vector and set them to zero?
Show older comments
I have the curve below. it is about a vector of 128 elements and i have to take just the peak and the rest must be replaced to zero. I think it is simple but i don't how because i am a beginner. I thought, i have to perhaps put the peak in anther vector and refill it with zeros but would it be take more time?

So I write the code below but i didn't get what i want, i am trying to creat a loop to test every element if it is smaller than zero and replace it to zero. The whole code workes without any changes in the curve and i get no error.
for i = 1 : length(Vector); % loop to test the elements of the curve from 1 to 128
VectorCurve = PPW ./ Rp + (C .* CPW); % the equation of the curve of 128 elements
if VectorCurve(i) < 0
VectorCurve (i) == 0;
end
end
Has anyone an idea to help me?
5 Comments
Paolo
on 2 Jul 2018
Do you just want the peak value or all positive values? You can remove replace negative values with zeros with logical indexing:
VectorCurve(VectorCurve<0) = 0;
Ruaa
on 2 Jul 2018
It is actually rare that you have to use a loop in matlab. Most likely, shifting the peak to 0 can be done in one line of code without a loop.
However, I have no idea what shifting the peak to 0 mean in practice. Please, give a numerical example of input and corresponging desired output.
Ruaa
on 2 Jul 2018
Ruaa
on 2 Jul 2018
Accepted Answer
More Answers (0)
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!