How to loop through table column comparing subsequent records but ignoring zero values?

1 view (last 30 days)
I would like to ensure subsequent column values (of wind power) only change by 20% per time step, except where there is a zero value. That is, [1am 2am 3am 4am 5am] [100, 200, 250, 0, 100] would become [1am 2am 3am 4am 5am] [100, 120, 144, 0, 100]. However, as soon as the loop reaches a zero, the rest of the column is set to zero, because 20% of zero is zero.
Here is the code I have tried. I just keep getting a list of zeros after reaching the first zero. I think I need logical indexing but can’t work out how to implement it in this instance.
Thank you for all suggestions.
WeatherT2 = readtable('C:\Windyield_data.csv','Delimiter','comma');
head(WeatherT2)
% WeatherT2 is table name.
% WeatherT2.timestamp and WeatherT2.windpower are the column names.
----------------------------------
numRows = height(WeatherT2)
for row = 2 : numRows
if WeatherT2.windpower(row) == 0
WeatherT2.windpower(row+1) = WeatherT2.windpower(row+1);
elseif WeatherT2.windpower(row) > WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row-1)*1.2;
else WeatherT2.windpower(row) <= WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row);
end
end
----------------------------------
numRows = height(WeatherT2)
for row = 2 : numRows
if WeatherT2.windpower(row) > 0
if WeatherT2.windpower(row) > WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row-1)*1.2;
end
end
end
----------------------------------
numRows = height(WeatherT2)
for row = 2 : numRows
while WeatherT2.windpower(row) ~= 0
if WeatherT2.windpower(row) > WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row-1)*1.2;
end
break
end
end
----------------------------------
numRows = height(WeatherT2)
for row = 2 : numRows
if WeatherT2.windpower(row) > WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row-1)*1.2;
else
WeatherT2.windpower(row) =0
break
end
end
----------------------------------
skipNum = [WeatherT2.windpower ~= 0];
numRows = height(WeatherT2)
for row = 2 : numRows
if ismember(row,skipNum) %if row is member of the skipNum array
if WeatherT2.windpower(row) > WeatherT2.windpower(row-1)*1.2
WeatherT2.windpower(row) = WeatherT2.windpower(row-1)*1.2;
end
end
end

Accepted Answer

Divyanshu
Divyanshu on 16 May 2023
Hi Diane,
According to the description provided, here are few assumptions that I have made:
  • If current value is 0 in the column, then the subsequent value should not be changed.
  • In each iteration the new value of current row is 120% of the previous row value and has nothing to do with the old value of current row.
Here is a demo script you can refer to, and feel free to modify it according to your use case:
windpower = [100 200 250 0 80 130 150]';
iszero = windpower == 0;
numRows = height(windpower);
for i=2:numRows
if(~iszero(i-1))
if(~iszero(i))
windpower(i) = windpower(i-1)*1.2;
end
end
end

More Answers (0)

Categories

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

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!