For loop Averaging Values Sequentially
Show older comments
Hi,
I'm working on analyzing some data that requires me to find the average of two numbers listed sequentially in a column in an 4000x9 array, where the 9th column needs populating via a script. Every 200 rows the data repeat the values in the 1st column. I want to average the values of a row in the first column and the value beneath it (row+1) and put it in the 9th column automatically, and then every time the values start to repeat, to just halve the value of that row. This is what I wrote:
kbo_array(:,9) = zeros;
kbo_array(1,9) = kbo_array(1,1)/2;
for i = kbo_array(2,1):kbo_array(4000,1),
if kbo_array(i,1) > kbo_array(i-1,1)
kbo_array(i,9) = (kbo_array(i,1) + kbo_array(i+1,1))/2;
else
kbo_array(i,9) = i/2;
end
end
But, I get the error: "Index in position 1 is invalid. Array indices must be positive integers or logical values." I've tried some variants on the inputs but none work. I know how to do this in Excel, but I should learn how to do this properly.
I've searched this site to figure out for/while loops and IF statements, but I'm a MatLab newbie and I don't fully understand the terminology and code in the answers, or why the solutions work. Can someone please explain what I'm doing wrong and offer a suggestion on how to fix it? Many thanks in advance!
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!