In an assignment A(I) = B, the number of elements in B and I must be the same.

조회 수: 6 (최근 30일)
Hi every body,
When I run this code:
t=-3:.1:3;
Go=1000;
Bc=5;
Pin=@(t)3*exp(-t.^2);
Ein = quadgk(Pin,-inf,inf);
for m=1:length(t);
z=t(m);
E1=quadgk (Pin,-inf,z)/Ein;
G(m)=Go/(Go-(Go-1)*exp(-E1*0.1));
Pout(m)=Pin(z)*G.*1;
Phi(m)=-0.5*Bc*log(G.*1);
Aout(m)=sqrt(Pout)*exp(i*Phi);
end
Aoutf=fftshift(fft(Aout,100000));
f = -(-100000/2:(100000/2-1)).*1/(0.01*100000);
Poutf = abs (Aoutf).^2;
plot(f,Poutf,'--')
xlim([-15 5])
I face this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Please help me, and thanks all in advance

채택된 답변

Matt Tearle
Matt Tearle 2012년 1월 9일
G is growing in the loop, so Pin(z)*G is a vector (for m >= 2). You can't assign a vector to the single value Pout(m), hence the error.
Perhaps you mean Pout(m) = Pin(z)*G(m)?
Also, you should preallocate these arrays, so they don't grow inside the loop:
tlen = length(t);
G = zeros(1,tlen);
Pout = zeros(1,tlen);
etc
EDIT TO ADD: Actually, assuming you are just trying to do element-by-element calculations here, you don't need to do all these in the for loop. The quadrature is the only thing that needs a loop, so try this:
t=-3:.1:3;
Go=1000;
Bc=5;
Pin=@(t)3*exp(-t.^2);
Ein = quadgk(Pin,-inf,inf);
for m=1:length(t);
z=t(m);
E1=quadgk (Pin,-inf,z)/Ein;
end
G=Go./(Go-(Go-1)*exp(-E1*0.1));
Pout=Pin(t).*G.*1;
Phi=-0.5*Bc*log(G.*1);
Aout=sqrt(Pout)*exp(1i*Phi);
Aoutf=fftshift(fft(Aout,100000));
f = -(-100000/2:(100000/2-1)).*1/(0.01*100000);
Poutf = abs (Aoutf).^2;
plot(f,Poutf,'--')
xlim([-15 5])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by