필터 지우기
필터 지우기

Problem with the implementation of the quadruple sum

조회 수: 1 (최근 30일)
Przemyslaw Gontar
Przemyslaw Gontar 2017년 12월 15일
답변: Walter Roberson 2017년 12월 18일
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