All dimensions but the one being concatenated (1) must be equal how to solve

I have error in CVX code
code is
clc
clear all
iterations = 10;
%% Parameters
Mr=4; % no.of BS antenna
K=16; % no. of RIS1 reflecting elements
U=4; % no. of uplink users
L=1; % no. of evesdroppers
P_T=db2pow(30); % total power budget
P_u=db2pow(10); % consume power at uplink transmission
P_d=db2pow(10); % consume power at downlink transmission
eta_i=db2pow(20); %amplification factor
epsilon=0.05; %estimation error
sigg=-120;
sigma_i=db2pow(sigg); % (watts) noise power
sigma_I=db2pow(sigg); %(watts) noise power
sigma_E=db2pow(sigg); %(watts) noise power
noise =1;
alpha= 2.1; %large scale fading
d0=1; % Reference distance in meter
PL_0=db2pow(-30); % pathloss
b_u=rand(K, Mr)+1j*rand(K, Mr); % Beamforming vector
%% Channels
f_Ku = rand(K, U) + 1j * rand(K, U); % uplink user to RIS1
disp(f_Ku) % display channel matrix
G_AK = rand(Mr, K) + 1j * rand(Mr, K); % RIS1 to BS
disp(G_AK) % display channel matrix
f_eK = rand(1, K) + 1j * rand(1, K); % RIS1 to Eve
disp(f_eK) % display channel matrix

%% Rician Factor
K_factor = 5; % no. of rician factor

%% Channel Coordinates
x_bs = 0;
y_bs = 0;
x_Ru = -70;
y_Ru = 0;
x_RIS1 = -50;
y_RIS1 = 50;
x_Lu = 0;
y_Lu = 100;
%% Channel Matrix and vectors
si = zeros(1, iterations);
for sim=1:10
fKr = sqrt((x_RIS1-x_Ru)^2 + (y_RIS1-y_Ru)^2); % distance form uplink user to RIS1
pathloss_fKr = sqrt(PL_0*(fKr)^(2.1)); % Large-scale pass loss from the user to RIS1

GAK = sqrt((x_bs-x_RIS1)^2 + (y_bs-y_RIS1)^2);  % distance from RIS1 to BS
pathloss_GAK = sqrt(PL_0*(GAK)^(2.1));    % Large-scale pass loss from the RIS1 to BS

feK = sqrt((x_Lu-x_RIS1)^2 + (y_Lu-y_RIS1)^2);  % distance from RIS1 to Eve
pathloss_feK = sqrt(PL_0*(feK)^(2.1));    % Large-scale pass loss from the RIS1 to Eve

%% Rician Channel Fading
%%ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi)
for u = 1:U
    f_Kr(:,:,u) = sqrt(1/2).*ricianCh([K 1], 5, fKr, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for f_Kr
    G_AK = sqrt(1/2).*ricianCh([Mr K], 5, GAK, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for G_AK
    f_eK = sqrt(1/2).*ricianCh([1 K], 5, feK, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for f_eK
end
%% Self Interferece(SI) at BS
rho = 10^-9;
J_s = 0.2;
P_SI = rho*P_u*norm(J_s).^2;

%% Now generating the phase shift atrix in RIS
% Create phase shift matrix
theta = rand(K, K) + 1j * rand(K, K);
S_j = ones(K, U);
t_Eu = 0.5;
gamma_u = (exp(t_Eu)-1);
for ite=1:iterations
    %% Optimization
    cvx_begin sdp
    variable S_u(Mr, Mr) hermitian
    variable mu_u nonnegative
    for u = 1:U
        S_j(u,:,:) = ones(size(b_u(u,:),1), size(b_u(u,:),1), U);
        A1(:,:,u) = gamma_u*theta*theta'*sigma_I;
        A2_sum(:,:,u) = ones(size(theta(:,u),1), size(theta(:,u), 1));
        A3(:,:,u) = theta*f_Ku*S_u*f_Ku'*theta';
        for j = 1:U
            if j ~= u
                if size(f_Ku, 2) == size(theta, 1) && size(S_j, 1) == size(theta, 1) && size(S_j, 2) == size(theta, 1)
                    A2_sum(:,:,u) = (gamma_u * (theta * f_Ku)' * S_j(:,j))' * f_Ku' * theta';
                end
            end
            A_u(:,:,K) = A1(:,:,u) + A2_sum(:,:,u) - A3(:,:,u);
            cu1 = gamma_u*sigma_E;
            cu2(:,:,K) = gamma_u*f_eK*theta*theta'*f_eK'*sigma_I;
            cu_sum(:,:,L) = ones(size(theta(u,:),1), size(theta(u,:),1));
            if j ~= u
                cu_sum = f_eK*theta*f_Ku*(S_j(:,j)*(f_Ku'*theta'*f_eK')')';
            end
            cu_3 = f_eK*theta*f_Ku*S_u*f_Ku'*theta'*f_eK';
            c_u = cu1+cu2(:,:,L)+cu_sum(:,:,L)-cu_3;
            I_u = sigma_i+norm(G_AK(u,:)*theta).^2 * sigma_I;
            G_u = theta*f_Ku;
            G_Au(:,:,u) = G_AK(u,:)*theta*f_Ku;
            numerator = trace(G_Au(:,:,u)*S_u*G_Au(:,:,u)');
            sum_term = ones(size(G_Au(:,:,u), 1));
            if j~=u
                sum_term = sum_term+G_Au(:,:,u)*(S_j(:,j)*G_Au(:,:,u))';
            end
            denominator = I_u + sum_term;
        end
    end
    obj1 = numerator / denominator;
    maximize obj1
    %  end
    %end
    subject to
    trace(S_u)<=P_T;
    trace(G_u*S_u*G_u')+norm(theta)^2*sigma_I<=P_u;
    Q = [A_u(:,:,K) +mu_u*eye(K),  A_u(:,:,K)*f_eK';
        f_eK* A_u(:,:,K) , c_u-mu_u*epsilon^2];


    S_u>=0;
    Q>=0;
    mu_u>=0;
    cvx_end

    %% Define the Data Rate
    R_AK = log(1 + abs( G_AK * theta * f_Ku).^2 * P_u ./ sigma_i*norm(G_AK*theta).^2 + P_SI + sigma_I);
    R_eK = log(1 + abs(f_eK*theta*f_Ku).^2 * P_u ./ sigma_i*norm(f_eK*theta).^2 + sigma_I);

    %% Calculate the secrecy rates
    R_k_sec = max(R_AK - R_eK);

    %% Calculate the sum of secrecy rates
    C = sum(R_k_sec);
    disp(size(C))
    disp(['The secrecy sum rate (SSR) is: ', num2str(C)]);
    si(sim, ite)= C;
end

end
Rician channel code is
function [ch,Ld] = ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi,noise)
if ~isequal(size(chSize), [1 2]) || ~isreal(chSize)
disp(‘ERROR! input chSize must be in size of 2x1 and real’);
disp('your chSize : ‘); disp(chSize);
error(’ ‘);
end
if ~isreal(ricianFactor) || ~isscalar(ricianFactor) || ricianFactor<0
disp(‘ERROR! input kappa must be a real scalar positive’);
disp(‘your kappa :’); disp(ricianFactor);
error(’ ‘);
end
if ~isreal(dist) || ~isscalar(dist) || dist<0
disp(‘ERROR! input dist must be a real scalar positive’);
disp(‘your dist :’); disp(dist);
error(’ ‘);
end
if ~isreal(pathLossRef) || ~isscalar(pathLossRef) || pathLossRef<0
disp(‘ERROR! input pathLossRef must be a real scalar positive’);
disp(‘your pathLossRef :’); disp(pathLossRef);
error(’ ‘);
end
if ~isreal(distRef) || ~isscalar(distRef) || distRef<0
disp(‘ERROR! input distRef must be a real scalar positive’);
disp(‘your distRef :’); disp(distRef);
error(’ ‘);
end
if ~isreal(pathLossExp) || ~isscalar(pathLossExp) || pathLossExp<0
disp(‘ERROR! input pathLossExp must be a real scalar positive’);
disp(‘your pathLossExp :’); disp(pathLossExp);
error(’ ‘);
end
if ~isreal(freq) || ~isscalar(freq) || freq<0
disp(‘ERROR! input freq must be a real scalar positive’);
disp(‘your freq :’); disp(freq);
error(’ ');
end
Ld = (pathLossRef*((dist/distRef)^(-pathLossExp)))/noise; %disp(['Ld : ', num2str(Ld)]);
waveLen = 3e8/freq; %disp(['waveLen : ‘, num2str(waveLen)]);
k = 2pi/waveLen; %disp([‘k : ‘, num2str(k)]);
N_ar = chSize(1); rowCnt = (0:(N_ar-1)).’; %disp(‘N_ar :’); disp(N_ar);
N_at = chSize(2); colCnt = (0:(N_at-1)).’; %disp(‘N_at :’); disp(N_at);
d_a = waveLen/2; %disp(‘d_a :’); disp(d_a);
ar = (1/sqrt(N_ar))exp(1jk
d_arowCntsin(phi)); %disp(‘ar :’); disp(ar);
at = (1/sqrt(N_at))exp(1jkd_acolCntsin(phi)); %disp(‘at :’); disp(at);
H_NLOS = randn(chSize) + 1j
randn(chSize); %disp(‘H_NLOS :’); disp(H_NLOS);
ch = sqrt(Ld/2)*(sqrt(ricianFactor/(1+ricianFactor))arat’ + sqrt(1/(1+ricianFactor))*H_NLOS);
end
error is
Error using cvx/cat
All dimensions but the one being concatenated (1) must be equal.

Error in cvx/vertcat (line 6)
y = cat( 1, varargin{:} );

Error in Uplink_opt_try1 (line 116)
Q = [A_u(:,:,K) +mu_u*eye(K), A_u(:,:,K)*f_eK’;

Did you look at the dimensions of all the things being concatenated, i.e., separated by commas, in the line which generated the error? I suspect reshape and/or repmat could fix the issue.

Note that CVX does not perform implicit expansion on CVX expressions, so some operations which would be legal in double precision MATLAB are not allowed for CVX variables or expressions.

Thank you sir I have solve this error but i am got new error
error is
Error using cvx_end
Your objective function is not a scalar.

Error in Downlink_opt (line 131)
cvx_end

Based on

numerator = trace(G_Au(:,:,u)*S_u*G_Au(:,:,u)');
sum_term = ones(size(G_Au(:,:,u), 1));
if j~=u
                sum_term = sum_term+G_Au(:,:,u)*(S_j(:,j)*G_Au(:,:,u))';
            end
            denominator = I_u + sum_term;
        end
    end
    obj1 = numerator / denominator;
    maximize obj1

It appears that numerator is a scalar, and denominator is a vector. So I don 't know why there wouldn’t be an error message when obj1 = numerator / denominator; is executed.

The CVX objective must evaluate to a real scalar. Based on the error message, your does not.

What is the result of
whos obj1

obj1 = cvx real affine expression (scalar)

Are you using CVX 2.2? If not, please do so. Do not use CVX 3.0beta, which is riddled with bugs.

If you are using CVX 2.2, please show the complete program, with all output. Include whos obj1 just prior to the maximize statement. I don’t see how you would get that error message if the objective function actually is cvx real affine expression (scalar).

Thank You sir I have solve this error

can you help me one more code

Code is
clc
clear all
iterations = 1;

%% Parameters
Mt=4; % no. of BS transmit antenna
N=16; % no. of RIS2 reflecting elements
D=4; % no. of downlink users
L=1; % no. of evesdroppers
P_T = db2pow(30); % maximum Power budget
P_u=db2pow(20); % consume power at uplink transmission
P_d=db2pow(20); % consume power at downlink transmission
eta_i=db2pow(20); % amplification factor
epsilon=0.02; % Bound level of estimation error
sigma_I=db2pow(-150); % Noise power
sigma_i=db2pow(-150); % additive noise power
sigma_E=db2pow(-150); % additive noise power at eve
noise=1;
alpha=2.1; % Pathloss exponent
alpha_E=3.8; % Pathloss exponent eve
d0=1; % Pathloss referece distance
w_d=rand(Mt, D)+1jrand(Mt, D); %Precoding vector
%disp(w_d) % display Precoding vector
PL_0=db2pow(-30); % pathloss
S_d_ini = w_d
w_d’;
%% Channels
G_NA = rand(N, Mt) + 1j * rand(N, Mt); % BS to RIS2
%disp(G_NA) % display channel matrix
h_dN = rand(1, N) + 1j * rand(1, N); %RIS2 to downlink user
%disp(h_dN) % display channel matrix
h_eN = rand(1, N) + 1j * rand(1, N); % RIS2 to Eve
%disp(h_eN) % display channel matrix

%% Channel Coordinates
x_bs=0; % X-axis coordinate at BS
y_bs=0; % Y-axis coordinate at BS
x_RIS2=50; % X-axis coordinate at RIS2
y_RIS2=50; % Y-axis coordinate at RIS2
x_LD=70; % X-axis coordinate at downlink users
y_LD=0; % Y-axis coordinate at downlink users
x_E=0; % X-axis coordinate at Eve
y_E=100; % Y-axis coordinate at Eve

si = [];
for sim=1:2
%% Distance between all channel

T_bs = sqrt((x_RIS2-x_bs)^2 + (y_RIS2-y_bs)^2);  % distance from transmit antennas BS to RIS2

LD = sqrt((x_LD-x_RIS2)^2 + (y_LD-y_RIS2)^2);  % distance from RIS2 to downlink users

E_LD = sqrt((x_E-x_RIS2)^2 + (y_E-y_RIS2)^2);  % distance from RIS2 to Eve

%% Rician Channel matrix
K_factor=5;  % no. of rician factor

%%ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi)
G_NA = sqrt(1/2).*ricianCh([N Mt], 5, T_bs, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for T_bs
h_dN = sqrt(1/2).*ricianCh([1 N], 5, LD, PL_0, d0, alpha, 2.4e9, pi/10, noise); % Rician Channel for LD
h_eN = sqrt(1/2).*ricianCh([1 N], 5, E_LD, PL_0, d0, alpha_E, 2.4e9, pi/10, noise); % Rician Channel for E_LD



%% Co-channel Interferece(CCI) at downlink transmission
varrho = 0.1;
l_u = 0.2;
CCI = varrho*P_u*norm(l_u)^2;

%% Now generating the phase shift matrix in RIS
phi=diag(rand(N, 1) + 1j * rand(N, 1));  % Phase shift matrix RIS2
S_l = ones(N,D);
t_E =  0.5;


gamma_d = (exp(t_E)-1);
fin=zeros(iterations, 1);
for ite=1:iterations
    %% Optimization
    cvx_begin sdp 
    variable S_d(Mt, Mt) hermitian 
    variable mu_d nonnegative;
    %expression Y(Mt+1, Mt+1)
    for d = 1:D
        A1(:,:,d) = gamma_d * phi * phi' * sigma_I^2;
        A_2_sum(:,:,d) = ones(size(phi(:,d), 1), size(phi(:,d), 1));
        A3(:,:,d) = phi * G_NA *S_d* G_NA' * phi;
        S_l(:,:,d) = ones(size(w_d(d,:),1), size(w_d(d,:),1));
        for l = 1:D
            if l ~= d
                if size(G_NA, 1) == size(phi, 1) && size(S_l, 1) == size(phi, 1) && size(S_l, 2) == size(phi, 1)
                    A_2_sum(:,:,d) = gamma_d * phi * G_NA * S_l(l,:) * G_NA' * phi';
                end
            end
        end
        A_d(:,:,d) =A1(:,:,d)+A_2_sum(:,:,d)-A3(:,:,d);
        C1 = gamma_d*sigma_E;
        C2 = gamma_d*h_eN*phi*phi'*h_eN'*sigma_I;
        CD_sum = ones(size(phi(l,:),1), size(phi(l,:),1));
        if l ~= d
        CD_sum  =  gamma_d * h_eN * phi * G_NA * S_l(l)' * G_NA' * phi' * h_eN';
        end

        C2 = h_eN * phi * G_NA * S_d * G_NA' * phi' * h_eN';
        c_d(:,:,d) = C1 + C2 + CD_sum - C2;
        I_d(:,:,d) = sigma_i+norm(h_dN*phi).^2 * sigma_I;
        H_A(:,:,d) = phi*G_NA;
        H_Ad(:,:,d)= h_dN*phi*G_NA;
        numerator = trace(H_Ad(:,:,d)*S_d*H_Ad(:,:,d)');
        sum_term = ones(size(H_Ad(:,:,d), 1));
        if l~=d
        sum_term = sum_term + H_Ad(:,:,d) * (S_l(:,l) * H_Ad(:,:,d))';
        denominator = I_d + sum_term;
        end
    end
        opt1 = numerator / denominator;
    

maximize(opt1)
subject to
trace(S_d) <= P_T;
real(trace(H_A(:,:,d)*S_d*H_A(:,:,d)')+norm(phi)^2*sigma_I) <= P_d;
Y = [A_d(:,:,d) + mu_d*eye(N),  A_d(:,:,d)*h_eN';
    h_eN*A_d(:,:,d),  c_d(:,:,d)-mu_d*epsilon^2];

real(Y) >= 0;
real(S_d)>= 0;
mu_d >= 0;
cvx_end
%% SINR

SINR_ld = norm(h_dN*phi*G_NA*w_d)^2*P_d / (sigma_i+norm(h_dN*phi)^2*P_d+CCI*sigma_I);
SINR_en = norm(h_eN*phi*G_NA*w_d)^2*P_d / (sigma_E+norm(h_eN*phi)^2*P_d*sigma_I);

%         SNR_bs = norm(b_u*G_AK*theta*f_Ku).^2*P_u / sigma_i;
%         SNR_ld = norm(h_dN*phi*G_NA*w_d).^2*P_d / sigma_i;
%         SNR_Eu = norm(f_eK*theta*f_Ku*b_u).^2*P_u / sigma_E;
%         SNR_Ed = norm(h_eN*phi*G_NA*w_d).^2*P_d / sigma_E;
%% Define the data rates
R_ld = log(1+SINR_ld);
R_en = log(1+SINR_en);

C_IB = R_ld;
C_IE = R_en;

%% Calculate the Secrecy rates

R_d_sec = max(C_IB  - C_IE);

end
end

Rician Channel code
function [ch,Ld] = ricianCh(chSize, ricianFactor, dist, pathLossRef, distRef, pathLossExp, freq, phi,noise)
if ~isequal(size(chSize), [1 2]) || ~isreal(chSize)
disp(‘ERROR! input chSize must be in size of 2x1 and real’);
disp('your chSize : ‘); disp(chSize);
error(’ ‘);
end
if ~isreal(ricianFactor) || ~isscalar(ricianFactor) || ricianFactor<0
disp(‘ERROR! input kappa must be a real scalar positive’);
disp(‘your kappa :’); disp(ricianFactor);
error(’ ‘);
end
if ~isreal(dist) || ~isscalar(dist) || dist<0
disp(‘ERROR! input dist must be a real scalar positive’);
disp(‘your dist :’); disp(dist);
error(’ ‘);
end
if ~isreal(pathLossRef) || ~isscalar(pathLossRef) || pathLossRef<0
disp(‘ERROR! input pathLossRef must be a real scalar positive’);
disp(‘your pathLossRef :’); disp(pathLossRef);
error(’ ‘);
end
if ~isreal(distRef) || ~isscalar(distRef) || distRef<0
disp(‘ERROR! input distRef must be a real scalar positive’);
disp(‘your distRef :’); disp(distRef);
error(’ ‘);
end
if ~isreal(pathLossExp) || ~isscalar(pathLossExp) || pathLossExp<0
disp(‘ERROR! input pathLossExp must be a real scalar positive’);
disp(‘your pathLossExp :’); disp(pathLossExp);
error(’ ‘);
end
if ~isreal(freq) || ~isscalar(freq) || freq<0
disp(‘ERROR! input freq must be a real scalar positive’);
disp(‘your freq :’); disp(freq);
error(’ ');
end
Ld = (pathLossRef*((dist/distRef)^(-pathLossExp)))/noise; %disp(['Ld : ', num2str(Ld)]);
waveLen = 3e8/freq; %disp(['waveLen : ‘, num2str(waveLen)]);
k = 2pi/waveLen; %disp([‘k : ‘, num2str(k)]);
N_ar = chSize(1); rowCnt = (0:(N_ar-1)).’; %disp(‘N_ar :’); disp(N_ar);
N_at = chSize(2); colCnt = (0:(N_at-1)).’; %disp(‘N_at :’); disp(N_at);
d_a = waveLen/2; %disp(‘d_a :’); disp(d_a);
ar = (1/sqrt(N_ar))exp(1jk
d_arowCntsin(phi)); %disp(‘ar :’); disp(ar);
at = (1/sqrt(N_at))exp(1jkd_acolCntsin(phi)); %disp(‘at :’); disp(at);
H_NLOS = randn(chSize) + 1j
randn(chSize); %disp(‘H_NLOS :’); disp(H_NLOS);
ch = sqrt(Ld/2)*(sqrt(ricianFactor/(1+ricianFactor))arat’ + sqrt(1/(1+ricianFactor))*H_NLOS);
end

In this error is
Error using cvx_end
Your objective function is not a scalar.

Error in Downlink_opt_try1 (line 130)
cvx_end

I don’t know what’s going on. Try running this in a new MATLAB session. If that doesn’t resolve matters, try reinstalling CVX 2.2 in a new MATLAB session. And if that doesn’t resolve it, try rebooting.

But, Sir I already using CVX 2.2 version

Did you try my other suggestions?

Have you tried a simpler example, such as one from the CVX Users’ Guide or the Example Library to see if that executes successfully?

1 Like