필터 지우기
필터 지우기

Index in position 2 exceeds array bounds (must not exceed 2).

조회 수: 1 (최근 30일)
Anjolaoluwa Bamtefa
Anjolaoluwa Bamtefa 2020년 7월 20일
댓글: madhan ravi 2020년 7월 20일
I am trying to write code to delete every column of a matrix that has 0 in the second row so I am only left with the columns that have values in that second row. The code works okay and does that but I keep getting the error "Index in position 2 exceeds array bounds (must not exceed 2)."
I need to use the values after and I keep running into the same problem. Can I get some help?
This is what my code looks like :
c= length(fill);
while c >= 1
if fill(2,c) == 0;
fill(:,c) = [];
c = length(fill);
else
c = c - 1;
end
end
fill is typically a 3 by 30 ish matrix (it changes for each set of data).

채택된 답변

madhan ravi
madhan ravi 2020년 7월 20일
Never name a variable fill !
ix = FiLL(2, :) == 0;
FiLL(:, ix) = []
  댓글 수: 1
Anjolaoluwa Bamtefa
Anjolaoluwa Bamtefa 2020년 7월 20일
Thank you very much! fill was a temporary name but I see now that it is a keyword.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bhupendra Prajapati
Bhupendra Prajapati 2020년 7월 20일
length function will give maximum of rows or columns in the matrix, you should use size function to calculate number of columns
You can use the code given below to do your task.
[r,c]=size(fill)
id=1;
while(id<=c)
if(any(fill(2,id)==0))
fill(:,id)=[];
c=c-1;
else
id=id+1;
end
end
  댓글 수: 1
madhan ravi
madhan ravi 2020년 7월 20일
As mentioned above , not a good idea to use fill as a variable name. And loop is totally unnecessary here.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by