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 个评论

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;
Thank you very for your answer:). I just want the peak values and tried your solution and it has worked.
But i have to also shift the peak to zero, that´s why i have to do it in a loop to know how many elements at the beginning of the curve are which i have to shift, is that right?
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.
Thank you Guillaume for your reply:).
Well at the beginning i solve this Problem individually with the code below, so that i saw the values of the curve on the plot and the i replace the negatives with zeros in the code.
VectorCurve = PPW ./ Rp + (C .* CPW); % the equation of the curve of 128 elements
VectorCurve(1:7) = 0;% Zero-Replace before the Peak
VectorCurve(50:128) = 0;% Zero-Replace after the Peak
VectorCurve = circshift(VektorCurve, [0,-7]);% Shifting the Peak to zero
But this was just for one curve and i have many of curves with differnet peaks. In Image is the final plot what i have to get. So, do you think i don't need to the loop?
It has worked with the Code below and without a loop. Thank you all very much:)
VectorCurve = PPW ./ Rp + (C .* CPW);% the equation of the curve of 128 elements
VectorCurve(VectorCurve<0) = 0;% Negative values replace to zero
FF = find(VectorCurve> 0); % find the Peak, which has only the positive values
VectorCurve = circshift(VectorCurve,[0,-FF]);% shifting the peak to zero

请先登录,再进行评论。

 采纳的回答

It has worked with the Code below and without a loop
This is indeed the way to do it. I would recommend one change though:
FF = find(VectorCurve> 0, 1); %only get the location of the first element > 0
As you wrote it, FF is a vector of all the indices for which your curve is > 0. So, in your earlier example, FF would be [8, 9, 10, 11, ..., 49]. You're then passing that vector to circshift which shift the respective dimensions by 8, 9, 10, 11, etc. position. Thankfully, since dimensions 3 to 44 are all singleton, it doesn't do anything but really, you only meant to pass the 8. Adding the 1 option to find means it only returns that 8 you wanted.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by