I'm trying to delete the following rows within a matrix - ones in which the second column contains a 0. Importing the text file Data.txt, which has about 2901 rows of time readings, and associated pressure readings. Some of the pressure readings are 0, and we need to delete these readings. My code gives an assignment dimension mismatch error and I have not clue why. Could someone please help me out? Thanks and here's my code:
for row = 1:length(Data)
if Data(row,2) == 0
Data(row,2) = [];
end
end

 Respuesta aceptada

James Tursa
James Tursa el 7 de Dic. de 2015
Editada: James Tursa el 7 de Dic. de 2015
When you delete rows in a for loop, the indexing changes so the for loop indexing is no longer valid. Instead of using a for loop, you can use logical indexing. E.g.,
Data(Data(:,2)==0,:) = [];
The reason this line of yours was failing was because you were trying to delete only one element of the matrix, and you can't do that:
Data(row,2) = [];

3 comentarios

Nikhil Srinivasan
Nikhil Srinivasan el 7 de Dic. de 2015
Ah I see what you mean, the length decreases, preventing me from accessing other elements. Thanks a ton
Nikhil Srinivasan
Nikhil Srinivasan el 7 de Dic. de 2015
How would I determine the number of corrupted points without a for loop though?
number_corrupted = sum(Data(:,2)==0);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 7 de Dic. de 2015

Comentada:

el 7 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by