Incorrect dimensions for matrix multiplication.

10 次查看(过去 30 天)
I'm very new with matlab and i have tried several times to learn and fix the warnings by myself but i ended up got nothing, so deeply need help for it. Here are the warning:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number
of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in testnoma (line 27)
nomaEncodedSymbols(subcarrier,user) =
sqrt(powerAlloc(user))*precodingMatrix(:,user)'*channelMatrix(:,:,user)*dataSymbols(user,subcarrier);
The code i tried to use is this one, thankyou in advise! Have a nice day!
% System parameters
nUsers = 10; % number of users
nTxs = 64; % number of transmit antennas
nRxs = 4; % number of receive antennas
nSymbols = 10; % number of symbols
noiseVar = 1e-3; % noise variance
nIterations = 30; % number of iterations for the algorithm
nSubcarriers = 16; % number of subcarriers
% Generate random data symbols
dataSymbols = randi([0,1], nUsers, nSymbols);
% Generate random precoding matrices
precodingMatrix = randn(nTxs, nUsers) + 1i*randn(nTxs, nUsers);
% Generate channel matrices
channelMatrix = (randn(nRxs, nTxs, nUsers) + 1i*randn(nRxs, nTxs, nUsers))/sqrt(2);
% Generate random power allocation coefficients
powerAlloc = rand(1, nUsers);
% Perform NOMA encoding
nomaEncodedSymbols = zeros(nSubcarriers, nUsers);
for subcarrier = 1:nSubcarriers
powerSum = 0;
for user = 1:nUsers
nomaEncodedSymbols(subcarrier,user) = sqrt(powerAlloc(user))*precodingMatrix(:,user)'*channelMatrix(:,:,user)*dataSymbols(user,subcarrier);
powerSum = powerSum + abs(nomaEncodedSymbols(subcarrier,user))^2;
end
for user = 1:nUsers
nomaEncodedSymbols(subcarrier,user) = nomaEncodedSymbols(subcarrier,user) / sqrt(powerSum);
end
end
% Generate random initial state and covariance
state = randn(2*nTxs*nRxs, 1);
covariance = eye(2*nTxs*nRxs);
% Perform joint detection with Kalman filtering
kalmanEstimates = zeros(nSymbols, nUsers);
for iteration = 1:nIterations
for subcarrier = 1:nSubcarriers
H = zeros(nRxs,nTxs);
for user = 1:nUsers
H = H + squeeze(channelMatrix(:,:,user))*diag(precodingMatrix(:,user));
end
H = H / sqrt(nTxs); % normalization
V = diag(nomaEncodedSymbols(subcarrier,:));
noise = sqrt(noiseVar)*randn(nRxs, 1);
y = H*V*dataSymbols(:,subcarrier) + noise;
% Perform Kalman filtering
statePrediction = H\state;
covariancePrediction = H*covariance*H' + noiseVar*eye(nRxs);
kalmanGain = covariancePrediction*H'*inv(H*covariancePrediction*H' + noiseVar*eye(nRxs));
state = statePrediction + kalmanGain*(y - H*statePrediction);
covariance = (eye(2*nTxs*nRxs) - kalmanGain*H)*covariancePrediction;
kalmanEstimates(subcarrier,:) = state(1:nUsers).';
end
end
% Compute BER
ber = sum(sum(abs(kalmanEstimates - dataSymbols).^2)) / (nUsers * nSymbols);

回答(2 个)

Amit Dhakite
Amit Dhakite 2023-4-13
Hi Gisella,
As per my understanding, you are getting an error due to incorrect dimensions for matrix multiplication.
After checking the dimensions of each matrix in your code's line 27, the observations are listed below:
% Size of sqrt(powerAlloc(user))
[1 x 1]
% Size of precodingMatrix(:,user)'
[1 x 64]
% Size of channelMatrix(:,:,user)
[4 x 64]
% Size of dataSymbols(user,subcarrier)
[1 x 1]
The problem is arising due to multiplication of precodingMatrix(:,user)' and channelMatrix(:,:,user), as there dimensions are not compatible for matrix multiplication.
[1 x 64] x [4 x 64]
To perform a successful matrix multiplication between matrices of size [a b] and [c d], the value of b must be equal to c. Thus, I recommend that you regenerate your matrices with the appropriate dimensions to satisfy this criteria. Also, try to use parenthesis to make sure which matrices gets multiplied first.

Bhanu Prakash
Bhanu Prakash 2023-4-13
Hi Gisella,
As per my understanding, you are facing some errors in your code and want to know the reason behind the error.
To perform matrix multiplication, the number of columns of first matrix must be equal to the number of rows of the second matrix.
Consider the code below, for your reference:
>> x=[1 2;3 4;5 6]
x =
1 2
3 4
5 6
>> y=[7 8;9 10]
y =
7 8
9 10
>> x*y
ans =
25 28
57 64
89 100
Here, the number of columns on matrix "x" (i.e., 2) is equal to the number of rows of matrix "y" (i.e., 2). So, the matrix multiplication is possible.
But, It can be clearly seen from your code that the number of columns of the matrix " precodingMatrix(:,user)'" (i.e.,64) is not equal to the number of rows of the matrix " channelMatrix(:,:,user) " (i.e.,4). This throws an error stating "Incorrect dimensions for matrix multiplication".
You can refer to the documentation of matrix operations, for more info:
Hope this answer helps you.
Thanks,
Bhanu Prakash.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by