필터 지우기
필터 지우기

In the following code if the condition is met i want to remove those two respective elements from the array 'New'. But i get an error saying 'Index exceeds matrix dimensions'.

조회 수: 1 (최근 30일)
New = [800,750,740,700,500,300];
radius=50;
for k=1:111
for i=1:length(New)
for j= (i+1):length(New)
d(i,j)= sqrt((a(k)-New(:,i)).^2+(b(k)-New(:,j)).^2); % 'a' and 'b' are two matrices of size 1*111
if (d(i,j)<=radius)
New(i) = [];
New(j) = [];
end
end
end
end

답변 (1개)

Stephen23
Stephen23 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
This is a common beginner mistake. If you remove the first element of a four element vector, then it resizes to have three elements. But if your code still thinks the vector has four elements and you try to access the fourth element, then you will get an error. That is basically what you are doing.
Two solutions:
  1. iterate over the vector backwards.
  2. build a logical mask within the loop, and after the loop use the mask to remove all relevant elements at once.
  댓글 수: 2
Eranja Noopehewa
Eranja Noopehewa 2018년 7월 27일
please tell me how i want to rearrange the code to get rid of this? (i.e- how the code looks like?)
Eranja Noopehewa
Eranja Noopehewa 2018년 7월 27일
please upload here how the code looks like after in order to get rid of this..

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by