Code generates one more column than needed

Hello!
I'm new to Matlab. For some reason, I can't generate 'mr' to be the same size as 'm'. 'm' comes out to be 1280 columns, whereas 'mr' is always 1281 columns.
'm' is supposed to be data of 1-0-1-0, and 'mr' is supposed to be data derived from four random bits.
Here is the relevant code:
clear all;
fs=320; % sampling rate
ts=1/fs; % sampling interval
fc=10; % carrier frequency
T=4; % total simulation time
D=1; % data pulse duration 1s
L=D/ts; % the number of samples for one data pulse
HL=ones(1,L); % for logic '1'
LL=-ones(1,L); % for logic '0'
m=[]; % set to empty vector for initiation
for i=1:T/D % Generate the message signal
if mod(i,2)==1
m=[m HL]; % dump the logic '1'
else
m=[m LL]; % dump the logic '0'
end
end
rnum=rand(1,4) > 0.5;
i=1;
for i=1:T/D
for j=1:L
if rnum(i) == 0
mr=[m 0];
elseif rnum(i) == 1
mr=[m 1];
end
end
end
Thanks in advance for assistance!

 采纳的回答

You have a typo in the second "mr" loop and you forgot to pre-initialize mr. So you basically either got a copy of m with a zero or a one attached, instead of an mr.
This is what it should look like:
>> fs=320; % sampling rate
ts=1/fs; % sampling interval
fc=10; % carrier frequency
T=4; % total simulation time
D=1; % data pulse duration 1s
L=D/ts; % the number of samples for one data pulse
HL=ones(1,L); % for logic '1'
LL=-ones(1,L); % for logic '0'
m=[]; % set to empty vector for initiation
for i=1:T/D % Generate the message signal
if mod(i,2)==1
m=[m HL]; % dump the logic '1'
else
m=[m LL]; % dump the logic '0'
end
end
rnum=rand(1,4) > 0.5;
i=1;
mr = []; %pre-initialize mr
for i=1:T/D
for j=1:L
if rnum(i) == 0
mr=[mr 0]; %You had an m in the brackets mr = [m 0];
elseif rnum(i) == 1
mr=[mr 1];
end
end
end

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by