MATLAB Answers

0

Please need help!! How to write this equation in terms of code?

Asked by Abdullah on 18 Oct 2015
Latest activity Commented on by Faisal Muhammad on 22 Oct 2015

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by Walter Roberson
on 19 Oct 2015

x = 1 : L;
px = arrayfun(@p, 1:L);
ex = 1/L * sum(px .* x) / sum(px);
If p is vectorized then you can use
x = 1 : L;
px = p(x);
ex = 1/L * sum(px .* x) / sum(px);

  0 Comments

Sign in to comment.


Answer by Faisal Muhammad on 18 Oct 2015
Edited by Walter Roberson
on 19 Oct 2015

p(x)=1+x^2 % some function
for x=1:L
ex=1/L*((p(x)*x)/(p(x))
end
By the way the function can be reduced by canceling p(x)

  3 Comments

Abdullah, Walter is right. Please check his solution.

Sign in to comment.