# I am trying to solve following error in my code, Error using .* Matrix dimensions must agree.

1 view (last 30 days)
MOHINI MODI on 25 Sep 2017
Edited: Cedric Wannaz on 25 Sep 2017
clear all;
EbN0_dB = 0:2.5:30;
EbN0 = 10.^(EbN0_dB/10); % convert from dB scale
Analytical_BER_BPSK = Q(sqrt(2*EbN0)); % BER of QPSK and BPSK
L = 1000;
Numerical_BER_BPSK = zeros(1,length(EbN0));
for i=1:length(EbN0) % run simulation for each EbN0 value
BPSK_error = 0; % to count number of errors with BPSK
noOfL = 0; % to keep track number of bit sequences
while BPSK_error < 100
N0 = 1/(EbN0(i)); % with Eb = 1, N0 = 1/(EbN0)
s = randi([0,1],1,L); % random bit sequence of length L
BPSK_RX_signal = 2*s - 1; % BPSK modulated signal
noise = sqrt(N0/2) * randn(1,L); % Gaussian noise;
% Generate the fading channel, it's a Gaussian random variables
channel = sqrt(1/2)*(randn(1,L) + 1i*randn(1,L));
BPSK_RX_signal = channel.*BPSK_RX_signal + noise; % receive BPSK signal
% Before doing demodulation, you do equalizer and co-phases the
% channel
BPSK_RX_signal = conj(channel).*BPSK_RX_signal;
% demodulate ASK signal with threshold at sqrt(2)/2
% demodulate BPSK signal with thresshold at 0;
BPSK_s = double(BPSK_RX_signal > 0);
BPSK_error = BPSK_error + sum(BPSK_s~=s); % count error with BPSK
noOfL = noOfL + 1;
end
Numerical_BER_BPSK(i) = BPSK_error/L/noOfL;
end
hold on
A_BPSK = semilogy(EbN0_dB,Analytical_BER_BPSK,'--');
N_BPSK = semilogy(EbN0_dB,Numerical_BER_BPSK,'s');
xlabel('Eb/N0 in dB');
ylabel('BER');

Cedric Wannaz on 25 Sep 2017
Edited: Cedric Wannaz on 25 Sep 2017
If you got this code from someone else and it should be working (because it used to work with the exact same parameters), my guess is that the person wrote it for/on a recent version of MATLAB that supports implicit expansion, and you are trying to run it on an older version that requires explicit calls to BSXFUN for performing operations with implicit expansion (..).
>> x = 1 : 5
x =
1 2 3 4 5
>> Y = repmat( 21:25, 4, 1 )
Y =
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
>> x .* Y
ans =
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
where you can observe an automatic expansion of x along dim 1, otherwise we could not perform this element-wise multiplication. On former version of MATLAB, we had to proceed as follows:
>> bsxfun( @times, x, Y )
ans =
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
If this is what is happening (based on your screenshots it could be), then you have to update the code and implement relevant calls to BSXFUN. For the line where you have the error, this should be:
BPSK_RX_signal = bsxfun( @times, channel, BPSK_RX_signal ) + noise; % receive BPSK signal
where both channel and noise are 8x1000 and BPSK_RX_signal is 1x1000.
If the code has never worked before, the error may be elsewhere.
EDIT: I don't understand how channel and noise can be 8x1000 with this code by the way, so the error should be elsewhere.
Cedric Wannaz on 25 Sep 2017
Edited: Cedric Wannaz on 25 Sep 2017
EDIT: I corrected my answer @mtimes -> @times, but it was not the issue.
If the expansion was an issue, my guess is that it was not the only one and that answering how you could have 8x1000 arrays in a code (above) that should not be building such arrays, is key to solving your issue.
The expansion cannot work on the line where your error is, and you don't understand what it does. In my example above, the element-wise multiplication (operator .* or function times) cannot work between x and Y, because they don't have the same size/dimension (we cannot match each element of x and each element of Y based on their position and perform multiplications: x is a row vector, Y is an array). But if we need to have an element-wise multiplication of x width each row of Y (notice that x and rows of Y have the same size/dimension), we can perform it with:
bsxfun( @times, x, Y ) % And simply x .* Y from R2016b on.
where the first argument is a handle on the function that we want to apply (here times) and BSXFUN detects that x and Y have a common dimension (2) and expands x along dimension 1 to match the number of rows of Y and perform the operation element-wise on two arrays with matching dimensions.
Now in the line associated with your new error, there is no matrix multiplication, it is logical indexing. Assuming that channel_max is a scalar, the following:
channel_power == channel_max
is an array of logicals with the same size as channel_power. I have no idea about the dimensions of all these variables that appear, but the size of the array of logicals should match the size of the array that is indexed, BPSK_RX_signal. This isn't a technical obligation, but I'd say that it is very unlikely that the person who wrote this code implemented a valid logical indexing with mismatching arrays.
It is difficult to know what is wrong without being able to run code and debug. If I were you though, I would focus on understanding why in your first question the arrays that I mentioned above are 8x1000 and not 1x1000, and whether it is valid or not.

### Categories

Find more on Logical in Help Center and File Exchange

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!