필터 지우기
필터 지우기

To Run A For-Loop

조회 수: 2 (최근 30일)
Pranjal Pathak
Pranjal Pathak 2012년 8월 16일
Hi,
Here is a coding where fx1 to fx16 is varying in steps of 2 and fy1 to fy16 is constant(2). Can anyone help me in writing the frequency from first row to fourth row using ' for-loop' so that it gives the same pattern as it is giving now.
The Code:
% Frequency Of The First Row
fx1=2;fy1=2;
fx2=4;fy2=2;
fx3=6;fy3=2;
fx4=8;fy4=2;
% Frequency Of The Second Row
fx5=10;fy5=2;
fx6=12;fy6=2;
fx7=14;fy7=2;
fx8=16;fy8=2;
% Frequency Of The Third Row
fx9=18;fy9=2;
fx10=20;fy10=2;
fx11=22;fy11=2;
fx12=24;fy12=2;
% Frequency Of The Fourth Row
fx13=26;fy13=2;
fx14=28;fy14=2;
fx15=30;fy15=2;
fx16=32;fy16=2;
M=zeros(512);
a=128;
b=128;
[x,y]=meshgrid(-1:2/127:+1, -1:2/127:+1);
circ=sqrt(x.^2+y.^2)<1;
% Generation Of Holograms
h1=circ.*(cos((x*pi*fx1)+(y*pi*fy1)))>0;
h2=circ.*(cos((x*pi*fx2)+(y*pi*fy2)))>0;
h3=circ.*(cos((x*pi*fx3)+(y*pi*fy3)))>0;
h4=circ.*(cos((x*pi*fx4)+(y*pi*fy4)))>0;
h5=circ.*(cos((x*pi*fx5)+(y*pi*fy5)))>0;
h6=circ.*(cos((x*pi*fx6)+(y*pi*fy6)))>0;
h7=circ.*(cos((x*pi*fx7)+(y*pi*fy7)))>0;
h8=circ.*(cos((x*pi*fx8)+(y*pi*fy8)))>0;
h9=circ.*(cos((x*pi*fx9)+(y*pi*fy9)))>0;
h10=circ.*(cos((x*pi*fx10)+(y*pi*fy10)))>0;
h11=circ.*(cos((x*pi*fx11)+(y*pi*fy11)))>0;
h12=circ.*(cos((x*pi*fx12)+(y*pi*fy12)))>0;
h13=circ.*(cos((x*pi*fx13)+(y*pi*fy13)))>0;
h14=circ.*(cos((x*pi*fx14)+(y*pi*fy14)))>0;
h15=circ.*(cos((x*pi*fx15)+(y*pi*fy15)))>0;
h16=circ.*(cos((x*pi*fx16)+(y*pi*fy16)))>0;
% An Array Of 4x4 Holograms(Concatenation)
% First Row Of Holograms
M(1:a, 1:b) = h1; %M(1, 1)
M(1:a, b+1:b*2) = h2; %M(2, 1)
M(1:a, 2*b+1:b*3) = h3; %M(3, 1)
M(1:a, 3*b+1:b*4) = h4; %M(4, 1)
% Second Row Of Holograms
M(a+1:a*2, 1:b) = h5;%M(1, 2)
M(a+1:a*2, b+1:b*2) = h6; %M(2, 2)
M(a+1:a*2, 2*b+1:b*3) = h7; %M(3, 2)
M(a+1:a*2, 3*b+1:b*4) = h8; %M(4, 2)
% Third Row Of Holograms
M(2*a+1:a*3, 1:b) = h9; %M(1, 3)
M(2*a+1:a*3, b+1:b*2) = h10; %M(2, 3)
M(2*a+1:a*3, 2*b+1:b*3) = h11; %M(3, 3)
M(2*a+1:a*3, 3*b+1:b*4) = h12; %M(4, 3)
% Fourth Row Of Holograms
M(3*a+1:a*4, 1:b) = h13; %M(1, 4)
M(3*a+1:a*4, b+1:b*2) = h14; %M(2, 4)
M(3*a+1:a*4, 2*b+1:b*3) = h15; %M(3, 4)
M(3*a+1:a*4, 3*b+1:b*4) = h16; %M(4, 4)
figure(1)
imagesc(M),colormap gray; axis image; axis off
title('Array Of 4x4 Holograms');
%Fourier Transform
z=fftshift(fft2(M,512,512));
q=abs(z);
figure(2)
imagesc(q); colormap gray; axis image; axis off
title('Focal Spots Of Grating Array');

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 8월 16일
편집: Andrei Bobrov 2012년 8월 16일
Please try code:
fx = 2:2:2^5;
fy = 2;
a=128;
b=128;
[x,y] = meshgrid(linspace(-1,1,128));
circ=sqrt(x.^2+y.^2)<1;
h = bsxfun(@times,circ,cos(pi*bsxfun(@plus,bsxfun(@times,x,reshape(fx,1,1,[])),fy*y)))>0;
n = size(h);
M = cell2mat(reshape(mat2cell(h,n(1),n(2),ones(n(3),1)),4,[]))';
figure(1)
imagesc(M),colormap gray; axis image; axis off
title('Array Of 4x4 Holograms'); %Fourier Transform
z=fftshift(fft2(M,512,512));
q=abs(z); figure(2)
imagesc(q); colormap gray; axis image; axis off
title('Focal Spots Of Grating Array');
OR with loop 'for..end'
fx = 2:2:2^5;
fy = 2;
a=128;
b=128;
c=512;
[x,y] = meshgrid(linspace(-1,1,128));
circ=sqrt(x.^2+y.^2)<1;
for j1 = numel(fx):-1:1
h{j1} = circ.*cos(pi*(x*fx(j1) + y*fy));
end
M = cell(c/a);
M(:) = h;
M = cell2mat(M');
figure(1)
imagesc(M),colormap gray; axis image; axis off
title('Array Of 4x4 Holograms'); %Fourier Transform
z=fftshift(fft2(M,512,512));
q=abs(z); figure(2)
imagesc(q); colormap gray; axis image; axis off
title('Focal Spots Of Grating Array');
  댓글 수: 1
Pranjal Pathak
Pranjal Pathak 2012년 9월 7일
Thanks a lot sir. It really reduced the size of my code. Once again, thanks!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by