Why am I not getting desired values using loop?

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]
br_peaks = 1×19
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

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)
Thank you for your answer. yeah, i want A&&(B||C). would it be the reason for negative values?
I think that "points" should be in matrix form because I uses nested loops?
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() )
Thanks agian. Unfortunately, this does not make sense in my case.
Torsten
Torsten on 17 Jul 2022
Edited: Torsten 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.
For example, supose br_peaks = [1 2 3 4 5 6 7] and take first t4 = 4 then we have to save these values (below)
point(1) = t4-t4
point(2) = t3-t4
point(3) = t2-t4
point(4) = t1-t4
point(5) = t5-t4
point(6) = t6-t4
point(7) = t7-t4
For t3 = 3,
point(1) = t3-t3
point(2) = t2-t3
point(3) = t1-t3
point(4) = t4-t3
.......
point(6) = t6-t3
so on ...
Yeah, you are right. I overwrote. I don't understand how should I avoid. I am a beginner in coding.
Torsten
Torsten on 17 Jul 2022
Edited: Torsten 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".
I want the following points written in the attached image
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?
Let say br_peaks = [t1, t2, t3, t4, t5, t6]
For each point, I substracting it from three forward points and three backward points, which is shown in each column.
For example, in the first column, points are for t1. Since there does not exist three points before t1 so it starts from 0 and goes for three forward points. Similarly, I did for each point and write in corresponding column.
Many Thanks.
Torsten
Torsten on 19 Jul 2022
Edited: Torsten 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.
Yes, this is what I want to get.

Sign in to comment.

 Accepted Answer

lbr = length(br_peaks);
points = nan(lbr,7); % each row is 1->lbr; cols for a row are nan filled
% could change to cells of variable length arrays
for iii = 1:lbr
k = 1;
for jj = max([1,iii-3]):min([iii+3,lbr]) % could rewrite loop to one statement instead
points(iii,k) = br_peaks(jj)-br_peaks(iii);
k = k+1;
end
end % after rewrite of inner loop probably could rewrite to one statement

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
points = 7×7
1 2 3 4 5 6 7 -1 2 3 4 3 6 7 -2 -1 3 4 2 3 7 -3 -2 -1 4 1 2 3 1 -3 -2 -1 5 1 2 1 2 -3 -2 -1 6 1 1 2 3 -3 -2 -1 7

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017a

Tags

Asked:

on 17 Jul 2022

Answered:

on 19 Jul 2022

Community Treasure Hunt

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

Start Hunting!