Problem with the implementation of the quadruple sum

1 次查看(过去 30 天)
Hello, I'm trying write code for quadruple sum:
where x and y are matrix NxN. Output matrix also is NxN. I wrote something like that:
function v = RCPM(x,y,a,b,N,mi,V,n)
m = n - 1;
v = zeros(N,N);
for i = 1:m
disp(i);
for j = 1:m
for k = 1:m
for l = 1:m
s = mi^((a(j) - a(k + 1))^2 + (b(i) - b(l+1))^2) ...
* cos(V * (x * (a(j) - a(k + 1)) + ...
y * (b(i) - b(l+1))));
v = v + s;
end
end
end
end
end
But looks like it don't work well. Could anyone have a look at whether I implemented the above formula well?
  3 个评论
Walter Roberson
Walter Roberson 2017-12-18
I see no reason to remove the function command. function is a good thing there.
Walter Roberson
Walter Roberson 2017-12-18
It is not obvious to me that the formula definitely intends algebraic matrix multiplication like you implemented by using the * operators. It seems plausible to me that element-by-element multiplication should be used instead -- but I cannot tell for sure either way.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-12-18
You are iterating 1:m for each variable, which is m iterations each. However, your formula have 0 to M, which is M+1 iterations each. You define m as N-1 . It is not at all obvious that m = M+1, which would require that M = N-2
You code a(j) - a(k + 1) and b(i) - b(l+1), corresponding to alpha_lx - alpha_lx1 and beta_ly - beta_ly1 . Anyone reading this is going to assume that you got confused with the variable name lx1 as thinking that should be lx+1 but it is a completely different variable name. None of the subscripts should have +1 in them.
It is confusing that you have used i, j, k, l as the variable names controlling the loops and not ly, lx, lx1, ly1 (or Ly, Lx, Lx1, Ly1 for readability)
(a(lx)-a(lx1)) and (b(ly)-b(ly1)) occur twice in the expression, so it would probably make sense to compute them into separate variables.
For efficiency, pull expressions out of the inner loops. For example V * (x * (a(j) - a(k + 1)) does not need to be recalculated for each iteration of for l since it does not involve l

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by