Unable to perform assignment because the left and right sides have a different number of elements.
1 view (last 30 days)
Show older comments
Ruslan Askarov
on 25 Jun 2023
Commented: Ruslan Askarov
on 26 Jun 2023
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)
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
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
0 Comments
Accepted Answer
Image Analyst
on 25 Jun 2023
Edited: Image Analyst
on 25 Jun 2023
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:
More Answers (1)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!