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

1 view (last 30 days)
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!

Accepted Answer

Star Strider
Star Strider on 29 May 2014
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.
  4 Comments
Tien T
Tien T on 29 May 2014
Very much appreciated!
I also realized that my original code was not working as expected due to the fact that bits is an array of 1000000x1 while waveformBPSK(1:numSamples:end) is an array of 1x1000000. The error can be avoided if the original initialization for bits is:
bits = randi([0 1],1,numBits);
Thanks again, Star!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!