Help to transform math formula to matlab
Mostrar comentarios más antiguos
Hello,
I am trying to transform this formula into matlab, but I can't use a for/while loop. X is an array with dimension n and every position receives this sum.

I tried like this, but I couldn't remove the loop here, can I achieve this without a loop?

I also tried to use arrayfun, but without success because every position of the array was receiving the same value.
7 comentarios
Dave B
el 14 de Nov. de 2021
Is there are part of the sum that you can pull out?
(is sum(100*j) different from 100*sum(j)?)
Matthew Lima
el 14 de Nov. de 2021
Dave B
el 14 de Nov. de 2021
That sounds like a good idea to me!
Matthew Lima
el 14 de Nov. de 2021
Dave B
el 14 de Nov. de 2021
There sure is! Break it down, now that you've got the i part out of the sum, where would you think you need a loop?
Matthew Lima
el 14 de Nov. de 2021
Ah I was trying to avoid doing it for you so that you could feel the satisfaction :) Glad it feels simple, hope it stays that way as you delve deeper!
note this is the simplified version of what you wrote:
x = log(1:n) * sum(exp((1:m).^2))
Respuestas (1)
Using the vectorization:
format long
m=5;
jj=1:m;
XSum1 = sum(log(jj).*exp(jj.^2))
% Or alternatively
Xs = log(jj).*exp(jj.^2);
XSum2=sum(Xs)
% Sum stepby-step:
cumsum(Xs)
1 comentario
Dave B
el 15 de Nov. de 2021
Note that i and j are not the same in the question, the correct answer here is:
x = log(1:n) * sum(exp((1:m).^2))
as discussed in the comments above.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
