How to solve this error : "index out of bounds because numel(U)=1"
2 次查看(过去 30 天)
显示 更早的评论
Hi Everybody,
I know this question has been answered a housand times, but no solution I could find seemed to work, so this is my last resort.
Complete Error : Attempted to access U(1001); index out of bounds because numel(U)=1. Error in Identifiant>identifier (line 92) K=Y(i)/U(i);
function identifier (Y,U,Pr)
data=guidata(gcbf);
a=Y(1);
b=Y(5);
i=1;
j=1;
while (b-a<Pr && i<length(Y))
i=i+1;
a=Y(i);
b=Y(i);
end
K=Y(i)/U(i);
Here is the function where the error seems to be.
And the definition of Y and U is as follows :
u=1;
U=u(:);
y=K*(1-exp(-t/tau))*U;
Y=y(:);
I understood that U has no 1001st element, because it doesn't have the same size as Y. I tried to put both as vectors, but of course it does nothing. I also tried to use the function reshape, but it doesn't seem to work either or I might be using it wrong.
Anyways if anybody has any idea on how to solve this issue i'd be glad to listen to it. Thank you!
0 个评论
采纳的回答
Jon
2021-11-22
With your definition of U, U has only one element. Note you first assign a scalar (one element) variable u=1, then you assign U to equal every element U = u(:) of u, but it only has one element, so now U also has only one element. So it doesn't make sense to ask for elements with indices greater than 1.
2 个评论
Jon
2021-11-22
U and Y currently both will have the same number of elements. They will each have just one element.
If you coul provide a small standalone example script to demonstrate/reproduce your problem I could probably give you some more specific guidance
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!