error using == plot vectors must be the same lengths

1 view (last 30 days)
I have this error: error using == plot vectors must be the same lengths,when I try to excute the following code
fn=[0:100];
nt=2;
mr=2;
NF=3960;
delta_f=25000;
h_MO = (randn(mr,nt)+j*randn(mr,nt))/sqrt(2);
C_mi=zeros(1,NF);
Cm=zeros(1,2);
for k=1:NF
for p=1:length(fn)
if (p>=1)&& (p<30)
PTx=-50;
else if p>30
PTx=-80;
end
end
N_Rx(p)=(1./p^2)+10^15.5;
lamda2=eig(h_MO*h_MO');
for i=1:nt
C=Cm(i)+log2(1+(PTx.*lamda2./(N_Rx(p).*nt)));
end
end
C_mo=C_mi(k)+delta_f*C;
end
plot(fn,C_mo);
grid on;
  1 Comment
Thorsten
Thorsten on 14 Sep 2015
Please format all your code and give the values of all variables such that we can run the code. For example, what are the values of mR, mT?

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 14 Sep 2015
Your code
for i=1:nt
C=Cm(i)+log2(1+(PTx.*lamda2./(N_Rx(p).*nt)));
end
is overwriting all of C each iteration of the loop. Perhaps you wanted
for i=1:nt
C(nt) = Cm(i) + log2(1+(PTx.*lamda2./(N_Rx(p).*nt)));
end
  5 Comments
Walter Roberson
Walter Roberson on 14 Sep 2015
for i=1:nt
C(nt) = Cm(i) + log2(1 + (PTx.*lamda2(nt) ./ (N_Rx(p).*nt)));
end
You were failing to index the eigen values.
kokomy
kokomy on 15 Sep 2015
How I can index the eigen value in the right way the loop above give an error Index exceeds matrix dimensions.

Sign in to comment.


Stephen23
Stephen23 on 14 Sep 2015
Edited: Stephen23 on 14 Sep 2015
Problem
When I run the code (using mR = 3 and mT = 7 because you did not give us these values) I eventually get fn as a 1*100 vector, and C_mo as a 3*1 vector. Clearly these have very different sizes, and so cannot be plotted against each other because the plot function requires that the X and Y vectors have the same number of values.
Do you expect fn and C_mo to be the same length?
The variable C_mo is completely redefined on every iteration of the main for-loop, without any indexing or enlargement. This means it stores only the result of the final loop, which means it will have whatever size C_mi(k)+delta_f*C has. Because C is created just a few lines above and has size 3*1 and the other parameters are scalar, then C_mo also has size 3*1.
And fn is simply defined as the vector fn=[0:100].
These two variables are never going to be the same size.
Also note that variable C also gets completely reassigned on every iteration of its loop, which means only the last iteration is used. The rest a simply a waste of processing time. And given how slow this script was, it wastes a huge amount of time.
Solution
Really the solution is very simple: don't write a huge amount of code without checking and testing every line while you are writing it, and then get surprised when that code does not work. Write each line, check it, make sure that the values are correct, make sure that the correct values are saved, make sure that it fits your concept.
There is ginormous difference between what you think the code is doing, and what the code is actually doing. Understand what each line is doing, and make sure that this is what you need it to do. This means writing line-by-line, not writing a huge block that is then too complicated to understand what it is doing.
  6 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 16 Sep 2015
The corrected code based upon the pdf is below. I had to make some guesses about the meaning of some of the notation, and I am almost certain that some of what was in the pdf was incorrect.
nt = 2;
mr = 2;
NF = 3960; %set by the problem definition
fn = linspace(0,100E6,NF);
delta_f = fn(2)-fn(1); %a little over 25 1/4 KHz
N_Rx = @(f) 1./(f.^2 + 10.^(-15.5)); %guess about intended meaning
H = (randn(mr,nt) + 1i*randn(mr,nt))/sqrt(2);
lamda = eig(H*H'); %there are going to be nt of them
Cn = zeros(1,NF);
for n = 1 : NF
f = fn(n);
if f < 30E6
PTx = -50;
else
PTx = -80;
end
Cnt = sum( log2(1+(PTx.*f.*lamda.*f)./(N_Rx(f).*nt)) ); %formula is questionable!
Cn(n) = delta_f .* Cnt;
end
C = sum(Cn);
plot(fn, real(Cn));
grid on;
  7 Comments
kokomy
kokomy on 17 Sep 2015
Edited: kokomy on 18 Sep 2015
I missed to covert N_Rx to dB in such way logs is working with positive numbers by using N_Rxdb(n)=10*log10(N_Rx)in the relation instead of N_Rx
as well we need to generate two value of lamda
Walter Roberson
Walter Roberson on 19 Sep 2015
With that correction, the current code is as below:
The slope is less steep than before but otherwise is the same. Flipping the sign of what is being multiplied just removes the + 1*i from the log2: it still gets further away from 0 faster when you make PTX more negative.
The code does generate two values of lamda. lamda in the code is going to be length nt, which is to say 2 for these settings. The sum() is adding the results for the different lamda.
nt = 2;
mr = 2;
NF = 3960; %set by the problem definition
fn = linspace(0,100E6,NF);
delta_f = fn(2)-fn(1); %a little over 25 1/4 KHz
N_Rx = @(f) 1./(f.^2 + 10.^(-15.5));
N_RXdb = @(n) 10*log10(n);
H = (randn(mr,nt) + 1i*randn(mr,nt))/sqrt(2);
lamda = eig(H*H'); %there are going to be nt of them
Cn = zeros(1,NF);
for n = 1 : NF
f = fn(n);
if f < 30E6
PTx = -50;
else
PTx = -80;
end
Cnt = sum( log2(1+(PTx.*f.*lamda.*f)./(N_RXdb(N_Rx(f)).*nt)) ); %formula is questionable!
Cn(n) = delta_f .* Cnt;
end
C = sum(Cn);
plot(fn, real(Cn));
grid on;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!