Why am I not getting desired values using loop?
Show older comments
I have data like
br_peaks =
Columns 1 through 15
0.0002 0.2118 0.4623 0.7067 0.9873 1.2611 1.5412 1.8127 2.0791 2.3712 2.6544 2.9500 3.2134 3.4818 3.7596
Columns 16 through 19
4.0461 4.3143 4.5847 4.8658
I want to make another data in spicific manner. For each value, I will subtract it from three forward points and three backward points and save it in another variable, points(say). I code this using for loops but I am getting only negative points but it should have positive points also.
my code is below. Could anyone help me where did mistake?
br_peaks = [ 0.0002 0.2118 0.4623 0.7067 0.9873 1.2611 1.5412 1.8127 2.0791 2.3712 2.6544 2.9500 3.2134 3.4818 3.7596...
4.0461 4.3143 4.5847 4.8658]
for iii = 1:length(br_peaks)
for jj = 1:length(br_peaks)
if iii<3 && br_peaks(jj)<br_peaks(iii+3)
points(jj) = br_peaks(jj)-br_peaks(iii);
elseif iii+3 > length(br_peaks) && br_peaks(jj)>br_peaks(iii-3)||br_peaks(jj)>br_peaks(iii)
points(jj) = br_peaks(jj)-br_peaks(iii);
elseif iii>=3 && br_peaks(jj)>br_peaks(iii-2) && br_peaks(jj)<br_peaks(iii+3)
points(jj) = br_peaks(jj)-br_peaks(iii);
end
end
end
12 Comments
Walter Roberson
on 17 Jul 2022
I can never remember whether && or || has higher priority, so if I need both at the same level, I use () to make sure I get the meaning I intend.
It turns out that || has the lowest priority at all, so that chain of A&&B||C is equivalent to (A&&B)||C but I suspect you wanted A&&(B||C)
Rakesh
on 17 Jul 2022
Walter Roberson
on 17 Jul 2022
Have you considered conv() the signal with [1 1 1 -1 1 1 1] ? That would subtract the central value from the sum of the three points before and the three points after? I wonder, though, if it would make more sense to conv() against [1/6 1/6 1/6 -1 1/6 1/6 1/6] ? Though for the boundary points you might want to calculate the number of points inside the window (which can be done with a different conv() )
Rakesh
on 17 Jul 2022
I think you will have to show us with a simple example what result you expect.
Take [1 2 3 4 5 6 7], e.g.
The best would be a stepwise solution protocol.
In the code above, you overwrite all the values obtained for points(jj) for 1<=iii<=length(br_breaks)-1 and only keep the values for points(jj) for iii = length(br_breaks). This cannot be what you want.
Rakesh
on 17 Jul 2022
Even in your explicit list above, you overwrite the point values for t4=4 when you take t3=3 in the second step.
Just tell us the final vector "points" and how you arrive there if you take br_peaks = [1 2 3 4 5 6 7]. Because at the moment, it seems to me that the result depends on the order in which you apply the operation to the array elements of "br_peaks".
Rakesh
on 19 Jul 2022
Shivam
on 19 Jul 2022
Can you provide more details by taking a small array of numbers, let say of size 6 and show how the operations is ot be performed one by one?
Rakesh
on 19 Jul 2022
[1 2 3 4 5 6 7]
Step 1, subtracting 1 from the three forward points:
[1 1 2 3 5 6 7]
Step 2, subtracting 2 from 1 backward and three forward points:
[-1 2 1 2 3 6 7]
Step 3, subtracting 3 from 2 backwards and three forward points:
[-2 -1 3 1 2 3 7]
...
Step 7, subtracting 7 from 3 backward points:
[1 2 3 -3 -2 -1 7]
Now you have 7 result vectors.
Is it that what you want ?
If not, modify the example accordingly.
Rakesh
on 19 Jul 2022
Accepted Answer
More Answers (1)
br_peaks = [1 2 3 4 5 6 7];
lbr = numel(br_peaks);
points = repmat(br_peaks,lbr,1);
for iii = 1:lbr
for jj = max(1,iii-3):max(0,iii-1)
points(iii,jj) = points(iii,jj) - br_peaks(iii);
end
for jj = max(lbr-2,iii+1):min(lbr,iii+3)
points(iii,jj) = points(iii,jj) - br_peaks(iii);
end
end
points
Categories
Find more on Loops and Conditional Statements 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!