Subscripted assignment dimension mismatch

조회 수: 1 (최근 30일)
DAVIES
DAVIES 2023년 8월 8일
편집: Walter Roberson 2023년 8월 8일
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에서 Waveform Generation에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by