MATLAB index out of bounds in loop with single-element array

  • Context: MATLAB 
  • Thread starter Thread starter dyfw
  • Start date Start date
  • Tags Tags
    Bounds Error Index
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
dyfw
Messages
1
Reaction score
0
Dear forummers,

I'm trying to solve a problem that is displayed in MATLAB below:-

? Attempted to access x(2); index out of bounds because numel(x)=1.

Error in ==> fit_fun at 5
Fit_fun_val= x(1)^2 + x(2)^2 + x(3)^2;

Error in ==> jack_immune at 47
Sel_Ab_cri(ksel)=fit_fun(Ini_Ab(:,ksel));

I understand that (Ini_Ab(:,ksel)) has to have 3 numbers/datas, but the coding is looping.
How am i able to modify the code as to have 3 numbers as well as a loop? Sorry for my poor explanation.

Part of the code is as below:-

for ksel=1:pop_size
Sel_Ab_cri(ksel)=fit_fun(Ini_Ab(:,ksel));
end

Thanks in advance.
PS: Do let me know if i need to post the full code. Thanks.
 
Physics news on Phys.org
dyfw said:
? Attempted to access x(2); index out of bounds because numel(x)=1.
You don't need to look at anything else. numel function returns the number of array elements. You attempted to access the 2nd element while the array has only one element.