Average matrix columns until convergence
Show older comments
I have a matrix of positions for separate runs (column 1 is run 1, column 2 is run 2, etc.). How can I take an average of the positions at each row for each run until the average converges within a set criteria? For example, if I only did 1 run:
x = [ 5.0040; 5.0072; 5.0120; 5.0173]
avg = [5.0040 5.0072 5.0120 5.0173]
If I have 2 runs:
x = [5.0040 5.1710; 5.0072 5.1724; 5.0120 5.1784; 5.0173 5.1896]
avg = [5.0875 5.0898 5.0952 5.1035]
Id like to take the difference of the previous average and the "new" average and select the MAX value. Once the max value is under a specified value, break out of the loop. This will tell me that I have done enough runs to know a confident position array.
Here's some code I have been working on:
x = A(:,2:5:end);
[height, width] = size(x);
S = 1;
prev = 0;
while S > 0.01
for i = 1:length(x)
for j = 1:width
avg(i) = mean(x(i,j))
diff = avg - prev
S = max(avg)
prev = avg
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!