Is this a right way to code nested sums?

:
Is the above code a correct way to code the following expression (note that in the code r is RRintervals, and i is n):
or it would be better to code it with nested for loops? Im asking because i cant really test it.

댓글 수: 5

Torsten
Torsten 2024년 1월 11일
편집: Torsten 2024년 1월 11일
I wonder why you define M as a symbolic array and not as a usual numerical array.
Is "RRintervals" a function or an array of values ? If "RRintervals" is an array, n > N must hold, I guess.
Marcello Calza
Marcello Calza 2024년 1월 11일
RRintervals is an array of doubles
Torsten
Torsten 2024년 1월 11일
편집: Torsten 2024년 1월 11일
Then what does RRintervals(n-j) and RRintervals(n-k) mean if n <= N, e.g. RRintervals(-3) ?
Or does the array M(n) only exist for n > N and M(n) can be set to NaN for n <= N ?
Marcello Calza
Marcello Calza 2024년 1월 11일
i dont get what is N in your example, but yes RRintervals cant have index < 0 , thats why i will probably just use nested for loops. Thanks for the replies.
Torsten
Torsten 2024년 1월 11일
편집: Torsten 2024년 1월 11일
Nested for loops don't help to make array indices positive. The N is the N in your graphics formula. Or is n=N ?

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

답변 (1개)

Balavignesh
Balavignesh 2024년 1월 11일
Hello Marcello,
To implement nested summations, you may employ nested 'for' loops or use array operations. The selection of the approach is contingent upon the nature of the problem at hand, particularly concerning the dependency of the summation indices. It appears that in your case, the summation indices 'j' and 'k' are interdependent on each other.
Consequently, I recommend utilizing nested 'for' loops to accomplish your objective. The code snippet provided below serves as an illustrative example to facilitate your comprehension of the concept.
% Define the limits of the loops
N = 5; % Upper limit for the inner loop
% Preallocate the vector with zeros
my_vector = zeros(1, N);
% Initialize a counter for the vector index
index = 1;
% Perform the outer loop
for i = 1:N
% Perform the inner loop
for j = i+1:N+1
% Insert the element at the current index
my_vector(i) = (my_vector(i) + 1)*j;
end
end
% Display the result
disp(my_vector);
1236 516 156 36 6
Refer to the following documentation links to have more information on:
Hope this helps!
Balavignesh S

댓글 수: 1

Marcello Calza
Marcello Calza 2024년 1월 11일
Yes the dependency between the indices is a problem so i think i will try to code it with nested for loops, thank you.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 1월 11일

편집:

2024년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by