Implement double summation in Matlab

Hi.
Could you help me implement these two equations in Matlab utilizing for loops?
SF is a matrix of 1400 rows and 120 columns.
N=120
t=1:1400
I think the first equation can be implemented with cumsum(SF,2) but I'm curious as to how it'd be with foor loops.
Thanks

답변 (2개)

Rik
Rik 2020년 12월 14일
편집: Rik 2020년 12월 14일

0 개 추천

The sigma symbol contains all syntactic elements that you need for a for loop:
y=0;%initialize to 0
for j=1:N
for i=1:j
y=y+f(i);
end
end

댓글 수: 4

Jean Pazos
Jean Pazos 2020년 12월 14일
편집: Jean Pazos 2020년 12월 14일
Thank you for your answer. Did you mean to type y=y+f(i)?
Also, in my case f is a 1400x120 matrix. Do I need a third for loop from t=1:1400, and should the argument of f be (t,i)? Like this
y=0;%initialize to 0
for t=1:1400
for j=1:N
for i=1:j
y=y+f(t,i);
end
end
end
Rik
Rik 2020년 12월 14일
Yes, that was a typo, which I now corrected.
The point is that a sigma is easy to convert to a for-loop. If you function of i (f) is actually also a function of t, you should include that as well, but only if your output is no longer a function of t.
It doesn't look like a heap of for-loops is your optimal strategy. Do you actually want to use this for something? I would strongly suggest using the sum function and cumsum. That will be much faster, as Matlab can optimize a lot of the calculations.
Jean Pazos
Jean Pazos 2020년 12월 14일
Yes, I wanted to understand how to implement summation with for loops.I think I've gotten the gist.
As for the equations in my OP. The first one is implemented with y=cumsum(SF,2), right?
How can I implement the product equation with functions and loops if necessary? I'm a little lost
Rik
Rik 2020년 12월 14일
편집: Rik 2020년 12월 14일
Maybe cumprod is what you're looking for there? If you think about what should happen mathematically, that operator mean to take the product of all elements, right? That would make it analogous to the sigma operator, which uses + on all operators. Note that while 0 is the null operand for sum (or is it called unit operand?), for a product that operand is 1.

댓글을 달려면 로그인하십시오.

aisha chauhan
aisha chauhan 2023년 5월 27일
편집: Image Analyst 2023년 5월 27일

0 개 추천

Please convert this equation into MATLAB code:

댓글 수: 1

Try a nested for loop:
theSum = 0;
for ii = 1 : Ncl
for ell = 1 : Nray
aHt = at(phi(ii, ell)^t, theta(ii, ell)^t)
theSum = theSum + alpha(ii, ell) * ar(phi(ii, ell)^r, theta(ii, ell)^r) * aHt^H
end
end
H = gamma * theSum;
But the equation seems weird, or maybe the nomenclature was just not explained. Like why are you raising a sub t to the H power when H is the result of the loop? To learn other fundamental concepts, invest 2 hours of your time here:
If you still have trouble, start your own question instead of posting in some 3 year old thread of @Jean Pazos's.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2020년 12월 14일

댓글:

2023년 5월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by