why do i recive Index in position 2 is invalid. Array indices must be positive integers or logical values

3 views (last 30 days)
Hi every one
i have tried to implement for loop in order to compute the model the idea is how can i compute this model at the first iteration then i have to compute it again in the folloing iteration
u=[p;s;c] this is the u column vector of my model with 153*1 dimension where p,s,c 51*1
iteration =10;
iteration(:,1)=u(:) here i would like to say that at first iteration compute the model from p,s,c inputs but i have got the error meassage as titled in the question so how can i do that
this is what iam trying to do
for i =1:iteration
[unkown, unknown,unknown ]=function([u(1),u(2),u(3)])
iteration(:,i+1)=u(:) here i want to increase the iteration counter
end
so how can i do
thank you
  1 Comment
hamza karim
hamza karim on 26 Oct 2021
I coudn't really understand your question but your code is quite odd.
first you are defining iteration as a scalar. Then by writing
iteration(:,1)=u(:)
you are simply overwritting your initial value of 10, here you would have iteration as a vector of size 153x1 as per your u vector.
then when you are writing your for loop, you say
for i=1:iteration
end
this does not make sense. If your objective is to make 10 iteration you simply write
for i=1:10
end
and if the iteration is as per your u vector size then
for i=1:length(u)
end
as your code is written
at the ligne
iteration(:,i+1)=u(:)
is as you are saying iteration(:,2)=u(:) which is impossible since iteration is initially a vector of size 153x1

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 26 Oct 2021
This error means that you are not using integer values to index your array. Position 2 indicates it is your column index that is incorrect.
A=rand(2);
% works
A(1,2)
ans = 0.2686
% doesn't work
A(1,1.5)
Index in position 2 is invalid. Array indices must be positive integers or logical values.

Community Treasure Hunt

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

Start Hunting!