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

1 view (last 30 days)
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

Accepted Answer

Steven Lord
Steven Lord on 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 Comments
Steven Lord
Steven Lord on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!