Array indices must be positive integers or logical values

1 view (last 30 days)
I am using the below code. When it gets to the line "left(j) = u - U(i-j+1)", it shows error "Array indices must be positive integers or logical values." How should I solve this issue? Any help would be appreciated!
clear all;
clc;
p=2
U = [0 0 0 1 2 3 3 3]
u=1
i=4
N = zeros(1,p+1)
left = zeros(1,p)
right = zeros(1,p)
N(1) = 1
for j=1:p
left(j) = u - U(i-j+1)
right(j) = U(i+j) - u
saved = 0
for r = 0:j-1
temp = N(r+1)/(right(r+1) + left(j-r))
N(r+1) = saved+right(r+1)*temp
saved = left(j-r)*temp
end
N(j+1) = saved
end
  2 Comments
Geoff Hayes
Geoff Hayes on 8 Apr 2020
Hossein - the above code seems to run fine for me...though I suppose if you chose different p and/or i then I can see how i-j+1 may be zero or negative. Since p is 2 and i is 4 then your only indices are 4 and 3 which are valid...but perhaps you want to make use of all the elements of U?
CS
CS on 8 Apr 2020
Thanks for quick response! I am trying to use the below functions to drive the basisfunction of a bspline. The functions are:
For finding the i, which then will be used in the basisfuns(p,i,u,U)
function [i] = findspan(p,U,u)
%return index i for knotspan, w.r.t. value u
%p degree, U knotvector, u value
%Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.)
%page 68
n = size(U,2) - (p+1);
if u == U(n+1)
i = n;
return
end
low = p;
high = n+1;
mid = ceil((low + high) / 2);
while u < U(mid) || u >= U(mid+1)
if u < U(mid)
high = mid;
else
low = mid;
end
mid = ceil((low + high) /2);
end
i = mid;
end
The basisfuns(p,i,u,U) is as
function [N] = basisfuns(p,i,u,U)
%returns functionsvalues of basisfunctions in point u
%p degree, i index,u value, U knotvector
%Piegl, Les and Tiller, Wayne. The NURBS book (2nd ed.)
%page 70
N = zeros(1,p+1);
left = zeros(1,p);
right = zeros(1,p);
N(1) = 1;
for j=1:p
left(j) = u - U(i-j+1);
right(j) = U(i+j) - u;
saved = 0;
for r = 0:j-1
temp = N(r+1)/(right(r+1) + left(j-r));
N(r+1) = saved+right(r+1)*temp;
saved = left(j-r)*temp;
end
N(j+1) = saved;
end
As can be seen, the values for u, i, and U will change accordingly.
My questions:
1) In the above two functions, the value of u should be given as an input. How abuot if I want to have the value of u as
u=0:5
?
2) Also, the formula for basisfuns is not recursive. How can I make it recursive to be like the following formula?
Your help is appreciated!

Sign in to comment.

Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing 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!