Unable to perform assignment because the left and right sides have a different number of elements.

조회 수: 1 (최근 30일)
Hello
I try to collect results of function in a vector, but there is error "Unable to perform assignment because the left and right sides have a different number of elements".
I tried also use "reshape (f,[],1), but it doesn't help.
Mr=2;
Mt=2;
Rt=eye(Mr);
Rr=[1 08;0.8 1];
K=0;
Hw=(randn(Mr,Mt)+1i*randn(Mr,Mt))./sqrt(2);
H=sqrt(1/(K+1))*sqrtm(Rr)*Hw*sqrtm(Rt');
N=1000;
gamma=zeros(Mt,1)
gamma = 2×1
0 0
for SNR=0:1:15
for k=1:1:N
Hw=(randn(Mr,Mt)+1i*randn(Mr,Mt))./sqrt(2);
X = Hw*Hw';
r=rank(X);
e = eig(X)
for i=1:r
p=1;
gamma(i)=(f(r,p,Mt,SNR,e));
Cclosed=sum(log2(1+SNR*gamma(i)/Mt*e(i)));
end
R(k)=Cclosed;
end
stem(SNR,mean(R))
xlabel("SNR [dB]");
ylabel("Ergodic Capacity [bits/s/Hz]");
hold on;
end
e = 2×1
0.2882 2.6803
Unable to perform assignment because the left and right sides have a different number of elements.
function Wp=f(r,p,Mt,SNR,e)
for i=1:1:r-(p-1)
wl=1/(r-(p-1))*(Mt+Mt/SNR*sum(1/e(i)));
end
for i=1:1:Mt
Wp(i)=wl-(Mt/SNR*1/e(i));
if Wp(i)<=0
Wp(i)=[];
end
if sum(Wp)~=Mt
p=p+1;
else end
end
end

채택된 답변

Image Analyst
Image Analyst 2023년 6월 25일
편집: Image Analyst 2023년 6월 25일
In
gamma(i)=(f(r,p,Mt,SNR,e));
f returns a variable Wp, which is a vector of MT elements. How are you thinking that you're going to stick it in the single, i'th element of gamma? Also, gamma is a built in function so don't use it as the name of your variable. Call it theGamma or something. Maybe theGamma should be a 2-D matrix with Mt columns
theGamma = zeros(1, Mt) % Call this BEFORE the loops start.
and then you can fill up the whole row
Wp = f(r,p,Mt,SNR,e); % A 1 x Mt row vector.
theGamma(i, :)= Wp; % Stuff Wp into the i'th row of theGamma
Also, see the FAQ for a discussion of the error:

추가 답변 (1개)

Parag Jhunjhunwala
Parag Jhunjhunwala 2023년 6월 25일

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by