How do I replace "for" loops by matrix multiplication? I am getting confused because of 4 "for" loops. Any suggestions?

조회 수: 3 (최근 30일)
Hello, the code is as follows.
value = zeros();
E2 = zeros();
dxdy = 1;
x1=1;y1=1;u1=1;v1=1;
for u = -1:1:1
for v = -1:1:1
for x = -1:1:1
for y = -1:1:1
value(x1,y1) = input_data(x1,y1)*exp( ( -1i*k*(((u-x)^2) + (v-y)^2)) / (2*0.2))*(dxdy);
y1 = y1+1;
end
x1 = x1+1;
y1 = 1;
end
S_rows = sum(value,2); %Adding components of matrix "value"
S_complete = sum(S_rows);
E2(u1,v1) = S_complete;
x1 = 1;
v1 = v1 + 1;
end
u1 = u1+1;
v1 = 1;
end
disp(E2)
  댓글 수: 4
Dhananjay Mishra
Dhananjay Mishra 2018년 10월 12일
If you see the program the values of u1 and v1 is reset everytime. Hence the input is 3*3. And the output is also 3*3

댓글을 달려면 로그인하십시오.

답변 (1개)

Miriam
Miriam 2018년 10월 12일
편집: Miriam 2018년 10월 12일
dxdy = 1;
[x,y,u,v] = ndgrid(-1:1);
value = input_data.*exp((-1i*k*(((u-x).^2) + (v-y).^2))/(2*0.2))*(dxdy);
S_rows = sum(value,2);
S_complete = sum(S_rows);
E2 = squeeze(S_complete);
disp(E2)
  댓글 수: 8
Dhananjay Mishra
Dhananjay Mishra 2018년 10월 12일
Now I don't get any error but the answer doesn't matches for both the codes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by