how can i sum over to variable in matlab?

Hi, i have a function as below in a for loop:
for i=1:20
end;
i'm trying to sum over variables N,N', as simple as it looks, it confused me? can any one help?
thanyou so much in advance

댓글 수: 4

Stephen23
Stephen23 2017년 1월 19일
편집: Stephen23 2017년 1월 19일
@Joseph: What are F, N, N', G, and K ? Arrays, functions, symbolic variables , ... ?
Joseph
Joseph 2017년 1월 19일
F, G and K are functions (which are called Wigner 3j symbols), and N and N' are array of numbers from 1 to 20.
Stephen23
Stephen23 2017년 1월 19일
@Joseph: and what have you written so far?
Guillaume
Guillaume 2017년 1월 19일
And can these function operate on matrices, or do they only accept scalars?

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

답변 (2개)

Jorge Mario Guerra González
Jorge Mario Guerra González 2017년 1월 19일
편집: Jorge Mario Guerra González 2017년 1월 19일

0 개 추천

you mean something like this....
I'm supposing F,K,G are random arrays,
are N and N' independent values??? because the notation N, N' is a little tricky for me.
F=rand(20,20);
G=rand(1,20);
K=rand(1,20);
suma=0;
for i=1:20
for j=1:20
suma=suma+F(i,j)*G(i)*K(j);
end
end
disp(suma);
%where i and j are your N and N'

댓글 수: 5

Never ever call a variable sum, since it prevents you from using the sum function. In particular, in your example, you could get rid of the loop and use that sum function to obtain the same result in just one line:
result = sum(sum(F .* G .* K.')); %R2016b only
%in versions < R2016b
%result = sum(sum(F .* bsxfun(@times, G, K.')));
However, I don't believe that's what the OP is asking.
Joseph
Joseph 2017년 1월 19일
F,G and K are not random arrays, they are some type of functions of (N,N'). and N=N'-1, and N'=(1:20)
@Guillaume sorry, that's true I wrote it as a fast script, I edit it rigth away.
@Joseph you should've specified that N=N'-1 in the question. So, since N=N'-1 the range of N is 0:19?. why does the equation you posted say N=1:20.
Joseph
Joseph 2017년 1월 19일
i apologize. corrected it.

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

Guillaume
Guillaume 2017년 1월 19일
편집: Guillaume 2017년 1월 19일

0 개 추천

Without any a priori knowledge of F, G, K, this is guaranteed to work:
[NN1, NN2] = ndgrid(0:19, 1:20); %all combinations of N and N'
FN = arrayfun(@F, NN1, NN2);
GN = arrayfun(@G, NN1);
KN = arrayfun(@K, NN2);
result = sum(sum(FN .* GN .* KN)); %or sum(FN(:) .* GN(:) .* KN(:)) which is probably faster but more cryptic.
If the F, G, K functions support implicit expansion or can work directly with vectors and matrices then the arrayfun lines would not even be needed.

카테고리

도움말 센터File Exchange에서 Quantum Mechanics에 대해 자세히 알아보기

질문:

2017년 1월 19일

편집:

2017년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by