I am getting error as "Matrix dimensions must agree" for the code shown below: Since I am new to matlab, so please help.

13 visualizaciones (últimos 30 días)
% Parameters
num_iterations = 100000; % Number of Monte Carlo iterations
M = 4; % Number of reflecting elements in IRS
K = 2; % Number of users
SNR_dB = 10; % Signal-to-noise ratio in dB
alpha_m1 = 1.5; % Nakagami-m shape parameter for link 1
alpha_m2 = 2; % Nakagami-m shape parameter for link 2
beta_m1 = 0.5; % Nakagami-m scale parameter for link 1
beta_m2 = 0.7; % Nakagami-m scale parameter for link 2
modulation_order = 4; % Modulation order (e.g., QPSK)
num_bits = 10000; % Number of bits per iteration
outage_threshold_dB = 5; % Outage threshold in dB
% Convert SNR from dB to linear scale
SNR = 10^(SNR_dB/10);
% Initialize counters for BER, SER, and outage
ber_count = 0;
ser_count = 0;
ergodic_capacity_sum = 0;
outage_count = 0;
% Monte Carlo simulation
for iteration = 1:num_iterations
% Generate Nakagami-m fading gains for link 1 and link 2
h1 = sqrt(beta_m1 * gamrnd(alpha_m1, 1/beta_m1, [1, M]));
h2 = sqrt(beta_m2 * gamrnd(alpha_m2, 1/beta_m2, [1, M]));
% Generate Rayleigh fading gains for direct links
g1 = sqrt(0.5) * (randn(1) + 1i * randn(1));
g2 = sqrt(0.5) * (randn(1) + 1i * randn(1));
% Compute effective channel gains with IRS reflection
phi = exp(1i * angle(h1) + 1i * angle(h2));
g_eff1 = g1 + phi * sqrt(SNR / M);
g_eff2 = g2 + phi * sqrt(SNR / M);
% Generate transmit symbols for both users
tx_symbols = randi([0, modulation_order-1], 1, K);
% Modulate transmit symbols
modulated_symbols = qammod(tx_symbols, modulation_order);
% Transmit through channels
received_symbols1 = g_eff1 * modulated_symbols(1);
received_symbols2 = g_eff2 * modulated_symbols(2);
% Add noise
noise1 = sqrt(0.5/SNR) * (randn(size(received_symbols1)) + 1i * randn(size(received_symbols1)));
noise2 = sqrt(0.5/SNR) * (randn(size(received_symbols2)) + 1i * randn(size(received_symbols2)));
received_symbols1 = received_symbols1 + noise1;
received_symbols2 = received_symbols2 + noise2;
% Compute received symbols with IRS reflection
received_symbols1 = received_symbols1 + phi * sqrt(SNR / M) * modulated_symbols(1);
received_symbols2 = received_symbols2 + phi * sqrt(SNR / M) * modulated_symbols(2);
% Demodulate symbols
demodulated_symbols1 = qamdemod(received_symbols1, modulation_order);
demodulated_symbols2 = qamdemod(received_symbols2, modulation_order);
% Count errors for BER and SER
ber_count = ber_count + sum(tx_symbols ~= demodulated_symbols1) + sum(tx_symbols ~= demodulated_symbols2);
ser_count = ser_count + (sum(tx_symbols ~= demodulated_symbols1) > 0) + (sum(tx_symbols ~= demodulated_symbols2) > 0);
% Compute ergodic capacity
ergodic_capacity_sum = ergodic_capacity_sum + log2(1 + abs(g_eff1)^2 / abs(g_eff2)^2);
% Check for outage
if abs(g_eff1)^2 / abs(g_eff2)^2 < 10^(outage_threshold_dB / 10)
outage_count = outage_count + 1;
end
end
% Calculate BER, SER, Ergodic Capacity, and Outage Probability
bit_error_rate = ber_count / (num_bits * num_iterations);
symbol_error_rate = ser_count / (K * num_iterations);
ergodic_capacity = ergodic_capacity_sum / num_iterations;
outage_probability = outage_count / num_iterations;
% Display results
fprintf('Bit Error Rate (BER): %.6f\n', bit_error_rate);
fprintf('Symbol Error Rate (SER): %.6f\n', symbol_error_rate);
fprintf('Ergodic Capacity: %.6f\n', ergodic_capacity);
fprintf('Outage Probability: %.6f\n', outage_probability);

Respuestas (1)

Sandeep Mishra
Sandeep Mishra el 6 de Dic. de 2024 a las 1:51
Hi Riaz,
I ran the provided code in MATLAB R2024b and encountered the same Array Dimension mismatch error.
Upon further investigation, I identified that the calculation of BER and SER errors was not performed correctly, which led to the error.
To resolve this issue, you can modify the error calculation method by comparing the elements of the 'tx_symbols' variable with the 'demodulated_symbols1' and 'demodulated_symbols2' variables.
Additionally, the ergodic capacity computation should incorporate element-wise square operations on the g_eff1 and g_eff2 variables to ensure accurate calculations.
Refer to the modified code snippet to resolve the issue:
% Count errors for BER and SER
ber_count = ber_count + sum(tx_symbols(1) ~= demodulated_symbols1) + sum(tx_symbols(2) ~= demodulated_symbols2);
ser_count = ser_count + (tx_symbols(1) ~= demodulated_symbols1) + (tx_symbols(2) ~= demodulated_symbols2);
% Compute ergodic capacity
ergodic_capacity_sum = ergodic_capacity_sum + log2(1 + abs(g_eff1).^2 / abs(g_eff2).^2);
% Check for outage
if abs(g_eff1).^2 / abs(g_eff2).^2 < 10^(outage_threshold_dB / 10)
outage_count = outage_count + 1;
end
I hope this helps!

Categorías

Más información sobre Propagation and Channel Models en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by