For loop skipping with if statement

I have a 'for loop' designed to go through graph data (col1 = time, col2 = y), with an 'if statement' so if it finds a value equal to 0 in col2 it will calculate an average between the values in col2 before and after the 0 at 'a' (the time in col1):
for a = 2:length(pk)-1
if pk(a,2) == 0
m = (pk(a+1,2) - pk(a-1,2))/(pk(a+1,1) - pk(a-1,1)); % calculate gradient of values before an after 'a'
pk(a,2) = pk(a+1,2) - m*(pk(a+1,1)-pk(a,1)); % calculate value at 'a' [there are no 0's in col1]
else
end
end
However in col2 of the data I have found consecutive runs of 0's which means I cannot apply the code above. How can I alter the code so as to skip over the runs of 0 to create average values for them also?

3 Kommentare

Torsten
Torsten am 11 Okt. 2022
if it finds a value equal to 0 in col2 it will calculate an average between the values in col2 before and after the 0 at 'a'
And why do you do that ?
Sam Hurrell
Sam Hurrell am 11 Okt. 2022
I'm comparing 2 different graphs, but they sometimes have different time points (hence why col2 sometimes equals 0). All I need is an average as a stand in.
Sam Hurrell
Sam Hurrell am 11 Okt. 2022
Bearbeitet: Sam Hurrell am 11 Okt. 2022
That's not really the question here though, your average still wouldn't work with runs of 0's

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 11 Okt. 2022
Bearbeitet: Jan am 12 Okt. 2022

0 Stimmen

This is a linear interpolation. This is implemented without a loop already:
pk = [1, 2; 3, 0; 4, 0; 5, 3; 7, 2; 10, 0; 13, 5];
plot(pk(:, 1), pk(:,2), 'bo');
hold('on');
miss = (pk(:, 2) == 0);
pk(miss, 2) = interp1(pk(~miss, 1), pk(~miss, 2), pk(miss, 1));
plot(pk(:, 1), pk(:, 2), 'r-');
plot(pk(miss, 1), pk(miss, 2), 'ro');

2 Kommentare

Sam Hurrell
Sam Hurrell am 11 Okt. 2022
I'm not trying to ignore the values equal to zero, I'm trying to calculate values for them, but I run into trouble when there are runs of 0
Jan
Jan am 12 Okt. 2022
Yes, and this is what I have posted already. The zeros are not ignored, but the average using the surrounding non-zero values is used. If there is one zero, the result is the same as with your code, but the interp1 approach allows a sequence of zeros also.
I've added red circles in the diagram to show the replaced zero values.

Melden Sie sich an, um zu kommentieren.

Torsten
Torsten am 11 Okt. 2022

0 Stimmen

The usual average would be
pk(a,2) = ( pk(a-1,2)*(pk(a,1)-pk(a-1,1)) + pk(a+1,2)*(pk(a+1,1)-pk(a,1)) )/(pk(a+1,1)-pk(a-1,1))
But I think this will result in the same problem you already have.
To compare two graphs, use interp1 to find values for the union of the time points of graph 1 and graph 2.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Gefragt:

am 11 Okt. 2022

Kommentiert:

Jan
am 12 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by