MATLAB Answers

how can i sum over to variable in matlab?

조회 수: 45(최근 30일)
Joseph
Joseph 19 Jan 2017
Edited: Guillaume 19 Jan 2017
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

표시 이전 댓글 수: 1
Joseph
Joseph 19 Jan 2017
F, G and K are functions (which are called Wigner 3j symbols), and N and N' are array of numbers from 1 to 20.
Stephen Cobeldick
Stephen Cobeldick 19 Jan 2017
@Joseph: and what have you written so far?
Guillaume
Guillaume 19 Jan 2017
And can these function operate on matrices, or do they only accept scalars?

로그인 to comment.

답변 수 (2)

Jorge Mario Guerra González
Jorge Mario Guerra González 님이 편집함. 19 Jan 2017
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

표시 이전 댓글 수: 2
Jorge Mario Guerra González
@Guillaume sorry, that's true I wrote it as a fast script, I edit it rigth away.
Jorge Mario Guerra González
@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 19 Jan 2017
i apologize. corrected it.

로그인 to comment.


Guillaume
Guillaume 19 Jan 2017
Guillaume 님이 편집함. 19 Jan 2017
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.

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by