How can i select certain elements from a vector and set them to zero?
Ältere Kommentare anzeigen
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 Kommentare
Paolo
am 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
am 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
am 2 Jul. 2018
Ruaa
am 2 Jul. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Descriptive Statistics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
