error using == plot vectors must be the same lengths

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

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)

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

when I correct and run the code I had this error: In an assignment A(I) = B, the number of elements in B and I must be the same. for this line C(nt)=Cm(i)+log2(1+(PTx.*lamda2./(N_Rx(p).*nt)));
@Walter Roberson: I explained that this line produces a non-scalar output (depending on the input parameters), so how should it now fit into one matrix element?
I think I was answering at the same time you were. And I did say "perhaps"; I did not try to chase down the size of the various vectors.
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.
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

mr=2 and nt=2 the matrix h_MO must be square, I want the correction of my code in the way that i can plot C_mo en function of fn
This is the code for a formula perhaps I am wrong in the programming of the equation
Please upload the algorithm or equation that you are trying to implement. We can help you find an efficient way to code it. To upload afile press the paperclip button, then both the Choose file and Attach file buttons. Otherwise you can give it to us as text in a comment (or last choice as an image in a comment).
In the attached file the formula that I m trying to implement,I hape that this help
I think you missed attaching the file.
Sorry I did'nt confirm the jointing

Sign in to comment.

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

Thank's for your reply, since we have two value for PTx why capacity in the graph does'nt fall in the value fn [30:100]
I am not certain. I need to sleep now, but I will return to this later.
I expected your answer Mr walter, the Capacity must be little fall down from [30E6:100E6]
I had found another error in the generation of eigenvalues lamda In principle, I have to find both value for i = 1 and for i=2 Ineed help in htis part
Your formula is working with the logs of negative numbers for all frequencies after the first, , The log of a negative number is log(abs(number))+pi*i . The numbers get more and more negative with increasing f, so their absolute values get larger . Eventually you switch from PTX = -50 to PTX = -80 which has the effect of making the numbers go negative slightly faster, so their absolute values get larger slightly faster. If you look carefully at the plot there is a slight jump upwards at 30 MHz.
If you wanted capacity to fall at 30 MHz you would have to be multiplying by a constant with a smaller absolute value, such as if you switched from -80 to -50. Even then you would simply get a slight dip.
I believe the formula you gave in the PDF is not an accurate rendition of the formula you need to use. It does not make sense to write PTX(f_n)lamda_i(f_n) if the (f_n) are intended as multiplications, not with PTX and lamda_i being scalars anyhow (it might make sense if matrix algebra was being used.) I suspect that PTX(f_n) might be intended to signify PTX applied to f_n, which is to say that f_n is to be examined and the appropriate PTX value of -50 or -80 is to be used. The lamba_i(f_n) I cannot account for as a function application though. And why is N_RX constructed in such a way as to be undefined at f_n = 0 ? Are complex values expected as a result? I don't think so! I think that what is being added to 1 in the log2 is intended to be either positive or in the range -1 to 0 so that you are adding log2() of fractions. But your definition of N_RX(f_n) ensures that N_RX(f_n) is very small (on the order of 1E-15 and dividing by that is going to give a very large result.
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
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.

Asked:

on 14 Sep 2015

Commented:

on 19 Sep 2015

Community Treasure Hunt

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

Start Hunting!