# Array indices must be positive integers or logical values

6 views (last 30 days)
CS on 8 Apr 2020
Commented: CS on 8 Apr 2020
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 CommentsShowHide 1 older comment
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?