MATLAB Answers

find average in for loop

1 view (last 30 days)
Travis nguyen
Travis nguyen on 10 Jun 2020
Edited: John McDowell on 11 Jun 2020
Please help me , i want to find average y of each incease unit pr.
for example . at pr =0 . y1 = a b c
at pr = 0.5 , y 2 = a1 b1 c1
at pr = 1 , y3 = a2 b2 c2
at pr = 1.5, y4 = a3 b3 c3
at pr = 2 , y 5 = a4 b4 c4
find average of y yavg = avg(A) Avg(B) Avg(C )
note A is sum of a , a1, a2, a3, a4,
how i can code that.
a = 2;
b = 0;
n =2 ;
pr =[b:1:a];
Pr =zeros(1,length(pr));
heght = zeros(1,3);
x = zeros(1,3);
y = zeros(1,3);
r = zeros (1,n);
c = zeros(11,3)
total = zeros(1,3);
rec = zeros(3,3)
for t=1:length(pr)
rec = pr(t);
Pr(t) = rec;
for i = 1:3
Dis = 100 /7 * (i-1);
heght = 10 - Pr(t);
x(i) = Dis;
for a = 1: n
r(a) = sqrt((20 - Pr(t)) ^ 2 + x(i) ^ 2);
end
y(i) = 100* sum(r);
end
end
  1 Comment
KSSV
KSSV on 10 Jun 2020
Not clear.

Sign in to comment.

Answers (1)

John McDowell
John McDowell on 10 Jun 2020
Edited: John McDowell on 11 Jun 2020
Hi Travis,
If I'm interpretting your question correctly, I think you want to make a list of average values within each time step of your loop (assuming t is time). You're really close:
for t = 1:Arbitrary
y(t,:) = [a(t), b(t), c(t)];
end
yavg = [mean(y(:,1), mean(y(:,2), mean(y(:,3)]; % Or whatever average you choose.
I also notice that in your description, you want your time step to go up by 0.5. In order to do that, you need to ammend your pr definition to:
pr =[b:0.5:a];
I hope this helps.
Cheers,
John
  2 Comments
John McDowell
John McDowell on 11 Jun 2020
Hi Travis,
I'm afraid that depends on your application! I interpretted your y1, y2, y3 notication as being at time steps 1,2,3 of your loop:
y(1,:) = [a(1), b(1), c(1)];
% Then as your time step increases to 2...
y(2,:) = [a(2), b(2), c(2)];
If this is not the case, can I ask you to try rephrasing your question? There's a lot of ambiguity in there. I'm sure it's obvious to you, but for us seeing it on this end, there's a lot of ways your question could be interpretted.

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!