Azzera filtri
Azzera filtri

why do I have this error in index

2 visualizzazioni (ultimi 30 giorni)
lakom Mariem
lakom Mariem il 2 Ott 2017
Modificato: OCDER il 2 Ott 2017
Hello Can anyone help me please I have this error in matlab in plotting
Error in prob (line 13)
Vc1(i)= f1(indx(i));
So can anyone help me
you can see my code
clear all;
indx=[0.0:4.99:50.0];
f1=@(x) Ceoscj(4,p);
f2=@(x) Cesow(4,p);
f3=@(x) Oscj(4,p);
f4=@(x) Swb(4,p);
f5=@(x) Ssj(4,p);
for i=1:size(indx,2)
Vc1(i)= f1(indx(i));
Vc2(i)= f2(indx(i));
Vc3(i)= f3(indx(i));
Vc4(i)= f4(indx(i));
Vc5(i)= f5(indx(i));
end
plot(indx,Vc1,'-o');
hold on;
plot(indx,Vc2,'-*');
hold on;
plot(indx,Vc3,'-s');
hold on;
plot(indx,Vc4,'-d');
hold on;
plot(indx,Vc5,'-m');
hold on;
leg=legend('Cesosj','Cesow','Oscj','Ssj');
set(leg,'location','best')
set(gca,'XTick',(0:5:50))
set(gca,'YTick',(0:10^-1:10^0))
xlabel('P [dB]');
ylabel('Secrecy Outage Probability');
thanks in advance
  3 Commenti
lakom Mariem
lakom Mariem il 2 Ott 2017
Error in @(x)Ceoscj(4,p)
Error in prob (line 13) Vc1(i)= f1(indx(i));
Jan
Jan il 2 Ott 2017
@lakom Mariem: Are you really sure that this is the complete message? It tells only, where the error occurs, but not, what the problem is. This is really unusual for error messages in Matlab.

Accedi per commentare.

Risposte (2)

Star Strider
Star Strider il 2 Ott 2017
You didn’t say what the error is, although I am guessing that it is ‘Undefined function or variable 'p'.’.
Note that here:
f1=@(x) Ceoscj(4,p);
the argument to ‘f1’ is ‘x’, and does not refer to ‘p’ (that is apparently undefined in your code).
  1 Commento
Jan
Jan il 2 Ott 2017
Modificato: Jan il 2 Ott 2017
@lakom Mariem: The p must be undefined after the clear all.

Accedi per commentare.


OCDER
OCDER il 2 Ott 2017
Modificato: OCDER il 2 Ott 2017
You are accessing a matrix using a non-integer index right here, which is not allowed. Index must be an integer > 0. Another issue is that your function handle uses 'x' as a variable, but you have 'p' instead. See comments below:
indx=[0.0:4.99:50.0]; %your index is not an integer > 0. What is the 0th element of a vector?
f1=@(x) Ceoscj(4,p); %variable is x, but you use p. To fix, use: f1 = @(x) Ceoscj(4,x)
for i=1:size(indx,2) %use numel. for i = 1:numel(indx)
Vc1(i)= f1(indx(i)); %here, f1(indx(1)) = f1(0). What is the 0th element of f1?
%Also, you do not preallocate Vc1, meaning this will be slow due to
% matrix creation, deletion, and copying every time the size changes in a loop.
end
plot(indx,Vc1,'-o'); %indx is used only as a x value, so maybe rename indx to x.
% Your "i" in the for loop counter is the real "index" variable.
Using all these loops and function handles is quite hard to maintain in the long run. I would instead try to make your functions like Ceoscj take in a vector input x and return a vector output. That way, you could do something like this:
x =[0.0:4.99:50.0];
Vc1 = Ceoscj(4, x);
plot(x, yVc1)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by