Matrix index is out of range for deletion

조회 수: 1 (최근 30일)
Frederik Reese
Frederik Reese 2022년 6월 27일
댓글: Star Strider 2022년 6월 27일
Hi,
i cant find a helpful solution for this error:
Matrix index is out of range for deletion.
If I run the selection again often times it works and deletes the values.
Here my code and attached the matrix.
for i=1:length(Zeit_Flutende_5000_BA_neu)
if isnan(Zeit_Flutende_5000_BA_neu(i,:))== 1;
Zeit_Flutende_5000_BA_neu(i,:)= [];
% FRI_5000_DOK(i,:)=[];
% Distanz5000(i,:)=[];
else
end
end
Thanks
  댓글 수: 2
KSSV
KSSV 2022년 6월 27일
Where you are getting the error? Which line?
Frederik Reese
Frederik Reese 2022년 6월 27일
line 3

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

채택된 답변

Star Strider
Star Strider 2022년 6월 27일
This is the problem when deleting elements in a loop, although there could be a different problem, such as addressing a column vector with row vector subscripting references.
It is better to use logical indexing to eliminate all the NaN values at once, without looping:
LD = load('Zeit_Flutende_5000_BA_neu.mat');
Zeit_Flutende_5000_BA = LD.Zeit_Flutende_5000_BA;
i = 1;
Zeit_Flutende_5000_BA_nonan(:,i) = Zeit_Flutende_5000_BA(~isnan(Zeit_Flutende_5000_BA(:,i))); % Logical Indexing
Zeit_Flutende_5000_BA_nonan(:,i) = rmmissing(Zeit_Flutende_5000_BA); % 'rmmissing'
These both give the same result (a 612x1 column vector with no NaN values). This is a column vector, not a row vector, so the original subscript references were incorrect. These are now correct.
The rmmissing function was introduced in R2016b. Use it if you have it, since it (and its friends) make prolbems like this easier.
.
  댓글 수: 2
Frederik Reese
Frederik Reese 2022년 6월 27일
Thanks.
how can I delete the same rows of other table (FRI_5000_DOK(i,:)=[]; Distanz5000(i,:)=[];) if isnan(Zeit_Flutende_5000_BA_neu(i,:))== 1;?
Thanks in advance
Star Strider
Star Strider 2022년 6월 27일
As always, my pleasure!
There was only one column vector in the ‘Zeit_Flutende_5000_BA_neu.mat’ file, so I am not certain what you want.
One option would be to create a xseparate logical vector that specifically selests all the values that are not NaN:
Lv = ~isnan(Zeit_Flutende_5000_BA(:,i));
Then the other tables (all of which must have the same number of rows as ‘Zeit_Flutende_5000_BA’) would be:
FRI_5000_DOK_nonan = FRI_5000_DOK(Lv,:);
Distanz5000_nonan = Distanz5000(Lv,:);
That should work, providing that the conditions I specified apply.
Name them whatever you like. I added ‘_nonan’ to emphasize that they are copies of the original tables retaining only the values where ‘Lv’ is.true
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by