MATLAB: Removing the array elements

eliminate element

%testing the elimation of the elements outside the boundary%
np=22;
L=10;
x=linspace(0,L,np);
%position(:,1)=x;
for i=1:np
if x(i) > 5
x(i) = [ ];
end
end
hello, i want to eliminate the value of x greater than 5 , i s not able yo do so. it says index array exceeds array bounds

Best Answer

  • #No Need of loop, efficient code.
    np=22;
    L=10;
    x=linspace(0,L,np);
    idx=find(x>5);
    x(idx)=[]
    If loop is needed, you can understand the issue from the comment section.