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

2 次查看(过去 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 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by