Help to transform math formula to matlab
1 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Sulaymon Eshkabilov
2021-11-15
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 个评论
Dave B
2021-11-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!