MATLAB: [Signal Processing] In an assignment A(I) = B, the number of elements in B and I must be the same.

bpskdigital signal processingsignal processing

The following code gives the error, "In an assignment A(I) = B, the number of elements in B and I must be the same." due to the last line of code.
numSamples = 10; % Number of samples
pulseShape = ones([0,numSamples]); % Generate a series of impulses
numBits = 1e6; % Number of input bits
bits = randi([0 1],numBits,1); % Generate a million random binary bits
bits(bits==0) = -1; % Modulate signal to BPSK
waveformBPSK = zeros([0,numBits .* numSamples]); % Creates signal spectrum
% Convolutes input pulses with spectrum at every sampled value; convert all
% bits at sampled values to be the corresponding input bit value.
waveformBPSK(1:numSamples:end) = bits;
In general, I would like to convert the zero values of the vector waveformBPSK at the values of numSamples to its corresponding bit values (generated randomly by bits).
It looks to me if the problem is because the program sees that bits is still a vector of 1 million values and cannot convert each value of the waveformBPSK to the bits vector. However, I would like to convert each zero value of the vector waveformBPSK at the sampled value of numSamples to each individual value of bits.
Cheers!

Best Answer

What would we do without accumarray?
Replace the last line in your code with these two:
z = accumarray( [1:numSamples:size(waveformBPSK,2)]', bits, [], @(x) x);
waveformBPSK = z;
They produce a column vector, so transpose to row if necessary.