For loop to repeat the loop with different set of value
Afficher commentaires plus anciens
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;
Réponses (1)
Bob Thompson
le 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 commentaires
salad9996
le 11 Mar 2020
Bob Thompson
le 12 Mar 2020
1i will be considered a variable. If you have it defined as such, that will be fine, but I wanted to make sure you weren't trying to do 1*i or something similar.
'I added the output variable indexed, it doesnt work.'
What do you mean by it not working? Do you receive a specific error, or is it simply still just outputing the final set of results? Have you tried putting a debug marker on the final part of the loop to see how the variables look after each iteration?
Unfortunately, it is difficult for us to help you troubleshoot more directly without having all of the inputs available. Would you be able to post a sample set for us to work with?
salad9996
le 12 Mar 2020
Bob Thompson
le 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.
salad9996
le 7 Avr 2020
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
