MATLAB Answers

For loop to repeat the loop with different set of value

9 views (last 30 days)
salad9996
salad9996 on 11 Mar 2020
Commented: salad9996 on 7 Apr 2020
Hi guys, I'm trying to run the simulation with multiple set of value,
SNRDB=6:2:16;
but what I got one same result for the whole row, I'm guessing it uses the same set of value to run the whole progress. Any suggestion on this??
clc
clear
format long
N=100000;
SNRDB=6:2:16;
I_da=sign(rand(1,11)-0.5);
Q_da=sign(rand(1,11)-0.5);
s=I_da+1i*Q_da;
ber_zf=zeros(1,length(SNRDB));
ber_mmse=zeros(1,length(SNRDB));
ber_n=zeros(1,length(SNRDB));
for i=1:length(SNRDB)
for j=1:N
n=1/sqrt(2*10^(SNRDB(i)/10))*(randn(1,11)+1i*randn(1,11));
h=(1/sqrt(2))*(randn(1,11)+1i*randn(1,11));
y=h.*s+n;
for k=1:3
if k==1
W(1,:) = ones(size(h));
elseif k==2
W(2,:)= 1./h;
elseif k==3
W(3,:)= conj(h)./((abs(h)).^2+n);
else
error('Unimplemented Equalizer');
end
end
z = W .* y;
z_=sign(real(z))+1i*sign(imag(z));
ber_n=ber_n+sum(s~=z_(1,:))/11;
ber_zf=ber_zf+sum(s~=z_(2,:))/11;
ber_mmse=ber_mmse+sum(s~=z_(3,:))/11;
end
end
ber_n=ber_n/N;
ber_zf=ber_zf/N;
ber_mmse=ber_mmse/N;

  0 Comments

Sign in to comment.

Answers (1)

Bob Nbob
Bob Nbob on 11 Mar 2020
You are seeing only the final results because you do not have your output variables indexed.
for i=1:length(SNRDB)
for j=1:N
n=1/sqrt(2*10^(SNRDB(i)/10))*(randn(1,11)+1i*randn(1,11)); % Might get an error with 1i
h=(1/sqrt(2))*(randn(1,11)+1i*randn(1,11));
y=h.*s+n;
for k=1:3
if k==1
W(1,:) = ones(size(h));
elseif k==2
W(2,:)= 1./h;
elseif k==3
W(3,:)= conj(h)./((abs(h)).^2+n);
else
error('Unimplemented Equalizer');
end
end
z = W .* y;
z_=sign(real(z))+1i*sign(imag(z));
ber_n(i,:)=ber_n+sum(s~=z_(1,:))/11;
ber_zf(i,:)=ber_zf+sum(s~=z_(2,:))/11;
ber_mmse(i,:)=ber_mmse+sum(s~=z_(3,:))/11;
end
end

  5 Comments

Show 2 older comments
salad9996
salad9996 on 12 Mar 2020
Thank you, this is the error. I am not sure what it mean by the right side is 2x6,
Bob Nbob
Bob Nbob on 12 Mar 2020
The error means that the right side of the equal sign is a 2x6 array of numbers.It's an issue because I didn't look closely enough at the equation.
Now that I looka bit more closely, it seems like you are adding the sum of some values to the previous values that came out of the loop. If you run this way, with no indexing, you will get an array that is the same size as the original with each element being a cumulative sum of the different loop values. I would guess from your original message that you would like to capture a different set of results for each loop. Because you have a 2D array as your base, I recommend using the third dimension to index your way through the results.
ber_n(:,:,i+1)=ber_n(:,:,1)+sum(s~=z_(1,:))/11;
ber_zf(:,:,i+1)=ber_zf(:,:,1)+sum(s~=z_(2,:))/11;
ber_mmse(:,:,i+1)=ber_mmse(:,:,1)+sum(s~=z_(3,:))/11;
I am also indexing the right side of each equation now, to make sure we are adding the calculated sum to the original values, which are contained the in the first 'sheet' of the respective output array.

Sign in to comment.