How can i select certain elements from a vector and set them to zero?
이전 댓글 표시
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
Paolo
2018년 7월 2일
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
2018년 7월 2일
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
2018년 7월 2일
Ruaa
2018년 7월 2일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
