Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Steven Lord
Steven Lord il 21 Ago 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 Commenti
Steven Lord
Steven Lord il 21 Ago 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.

Accedi per commentare.

Più risposte (0)

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by