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

1 回表示 (過去 30 日間)
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

採用された回答

Divyanshu
Divyanshu 2023 年 5 月 16 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by