Matrix index is out of range for deletion

3 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 27 Jun 2022
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 Comments
Star Strider
Star Strider on 27 Jun 2022
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
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!