Subscripted assignment dimension mismatch

5 次查看(过去 30 天)
function [power_alloc, sum_rate] = power_allocation_noma(channel_gains, target_rates, total_power)
% channel_gains: Matrix of channel gains for each user and subchannel
% target_rates: Target rates for each user
% total_power: Total available power
[num_users, num_subchannels] = size(channel_gains);
% Initialize power allocation matrix
power_alloc = zeros(num_users, num_subchannels);
for u = 1:num_users
% Calculate interference from other users on each subchannel
interference = sum(power_alloc(:, setdiff(1:num_subchannels, u)) .* channel_gains(:, setdiff(1:num_subchannels, u)), 2);
% Calculate power allocation using water-filling algorithm
lagrange_multiplier = (target_rates(u) - interference) / channel_gains(u, u);
allocated_power = max(lagrange_multiplier, 0);
% Allocate the calculated power to all subchannels for the user
power_alloc(u, :) = allocated_power * ones(1, num_subchannels);
end
% Calculate sum rate
sum_rate = sum(log2(1 + power_alloc .* channel_gains), 'all');
I get this error:
subscripted assignment dimension mismatch.
Error in power_allocation_noma (line 20)
power_alloc(u, :) = allocated_power * ones(1, num_subchannels);
Error in main2 (line 13)
[power_alloc, sum_rate] = power_allocation_noma(channel_gains, target_rates, total_power);

回答(1 个)

Florian Bidaud
Florian Bidaud 2023-8-8
编辑:Florian Bidaud 2023-8-8
This is because your interference variable is a 1*num_users vector, leading to lagrange_multiplier also being a 1*num_users vector. Thus allocated_power * ones(1, num_subchannels) is a num_subchannels*num_users matrix.
In the end you try to affect a num_subchannels*num_users matrix to a 1*num_subchannels vector.
The dimensions of the left and right side of the "=" are different
I think you can have what you seek with changing allocated power definiton to :
allocated_power = max(max(lagrange_multiplier, 0));

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by