How to write a summation code when the k is not equal to j
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
yang-En Hsiao
am 17 Jan. 2019
Kommentiert: Debnath Goswami
am 28 Sep. 2020
I want to write the code like this,and the rage of k is 1 to K,but i don't know how to write the " j is not equal to k" this summation code,can anyone show me how to write it ?
![不等於.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/144647/%E4%B8%8D%E7%AD%89%E6%96%BC.png)
Akzeptierte Antwort
Image Analyst
am 3 Feb. 2019
yang-En, haven't heard from you. If the vectorized approach above is to confusing to you then try this simple and intuitive for loop. It basically uses the "continue" command to skip interations where j = k.
numPoints = 3; % Whatever...
lambda = randi(9, 1, numPoints) % Whatever...
f = randi(9, 1, numPoints) % Whatever...
h = randi(9, 1, numPoints) % Whatever...
K = length(lambda)
outerSum = 0;
for k = 1 : K
innerSum = 0;
% Do the inner sum over j with j not equal to k
for j = 1 : length(h)
if j == k
continue; % Skip this iteration
end
innerSum = innerSum + h(j) * f(k);
end
outerSum = outerSum + lambda(k) * innerSum;
end
outerSum % Spew out to the command window so we can see what it is.
0 Kommentare
Weitere Antworten (1)
Torsten
am 17 Jan. 2019
If all arrays involved have K elements, you can use
h = ...;
lambda = ...;
f = ...;
S = sum(h);
H = S-h ;
result = sum(lambda.*f.*H)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!