Matrix index is out of range for deletion

2 次查看(过去 30 天)
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

采纳的回答

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