how can i sum over to variable in matlab?

59 ビュー (過去 30 日間)
Joseph
Joseph 2017 年 1 月 19 日
編集済み: Guillaume 2017 年 1 月 19 日
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 日
@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 日
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 件のコメント
Jorge Mario Guerra González
Jorge Mario Guerra González 2017 年 1 月 19 日
@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 日
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.

カテゴリ

Help Center および File ExchangeQuantum Mechanics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by