How to store the value of y in the loop being executed?

2 views (last 30 days)
I'm making a code and I don't know how to get the values that are being made inside the loop.
How do I see what the y results are?
Can anybody help me ?
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
for k=1:N
if Bit_wat ==1
y = s * exp(1i * pi/4);
else if Bit_wat ==0
y = s * exp(1i *(- pi/4));
end
end
end

Answers (2)

KSSV
KSSV on 3 Sep 2020
This is how you save y but you need to check your logic.
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
y = zeros(1,N) ;
for k=1:N
if Bit_wat ==1
y(k) = s * exp(1i * pi/4);
elseif Bit_wat ==0
y(k) = s * exp(1i *(- pi/4));
end
end
  2 Comments
adriane duarte
adriane duarte on 3 Sep 2020
Thank you KSSV.
I did it this way but the u only stores zeros.
It does not store the values of the loop.
Thank you very much for your attention.
then can I take another question?
KSSV
KSSV on 3 Sep 2020
Edited: KSSV on 3 Sep 2020
It is because, if-else condition is never executed and the intialized y is as it is. Thats why I said you need to think of your code. You should follow like below:
% BPSK
M = 2;
% number of bits or symbols
N = 10^4;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
y = zeros(N,1) ;
y(Bit_wat==1) = s(Bit_wat==1) * exp(1i * pi/4);
y(Bit_wat==0) = s(Bit_wat==0) * exp(1i * -pi/4);

Sign in to comment.


Tamir Suliman
Tamir Suliman on 3 Sep 2020
i think yo have so many end after the if statement I modified the number of bits per just to speed it up
clc
% BPSK
M = 2;
% number of bits or symbols
% N = 10^4;
N = 10^1;
% Generates random bits
Bits_ale = randi([0 M-1],N,1)>0.5;
% BPSK modulation 0 -> -1; 1 -> 1
s = 2 * Bits_ale-1;
% Generates random watermark bits
Bit_wat = randi([0 M-1],N,1)>0.5;
for k=1:N
if (Bit_wat ==1)
y = s * exp(1i * pi/4);
else (Bit_wat ==0)
y = s * exp(1i *(- pi/4));
end
end
y

Community Treasure Hunt

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

Start Hunting!