Filter löschen
Filter löschen

problem in command window : "Subscript indices must either be real positive integers or logicals"

2 Ansichten (letzte 30 Tage)
how do i resolve this??
function [ x] = zignrinv(c )
v=9.91256303526217e-3;
r=3.442619855899;
x(0)=(v/(nrmlpdf( r )));
x(1)=r;
x(c)=0;
for i=2:c
x(i)=sqrt(-2*log((v/x(i-1))+nrmlpdf(x(i))))
end
for i=0:c
zigr(i) =(x(i+1)/x(i));
end
end
and the function "nrmlpdf" is as folows;
function [ y ] = nrmlpdf( x )
y=exp(-x^2/2);
end

Akzeptierte Antwort

Steven Lord
Steven Lord am 21 Aug. 2021
There's no such thing as element 0 of an array in MATLAB. The first element of an array is element 1. Therefore line 4 of your function cannot work.
Modify your code so it uses 1-based indexing not 0-based indexing.
  3 Kommentare
Steven Lord
Steven Lord am 21 Aug. 2021
The last element of x to which your first for loop assigns is x(c). Your second for loop tries to access x(c+1) which doesn't exist. As written the last element you could create in the vector zigr is element zigr(c-1).
If x was a 5 element vector:
x = 1:5;
what would you expect zigr(5) to be and why? zigr(4) would be 5/4.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by