How to solve this error : "index out of bounds because numel(U)=1"

3 views (last 30 days)
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!

Accepted Answer

Jon
Jon on 22 Nov 2021
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 Comments
Younes Alaoui
Younes Alaoui on 22 Nov 2021
Ok, so how should I do if I want U to have as much elements as Y? I know there is a way to do it easily, but I forgot.
Jon
Jon on 22 Nov 2021
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!