MATLAB: How to delete specific rows in this matrix

delete rowsmatrix

So I have a set of data with which I converted into a matrix. In this matrix I have to delete rows who's average is less than 50. I run a for loop like this:
filename = '/home/kelm/uegabe/Alle/Muringayi/Tactile Sensor Robot Measurements/S1M1/2017-7-18_111304.txt';
m = importdata(filename);
m(:,1) = [];
for i = 1:size(m,1)
avg = mean(m(i,:));
if avg < 50
m(i,:) = [];
continue
end
end
However, all I get back is index exceeds matrix dimensions. I am also able to cut down on some of the rows who's average is less than fifty because the number of rows in my matrix goes from 717 to 426, but there are still more that remain. Does anyone know where I am going wrong and how I can delete the appropriate rows?

Best Answer

Yes, you will get an index exceeds matrix dimension. Your loop is set to ends at size(m, 1) as it was when you started. However, as soon as you delete a row, the size of the matrix is no longer that original size(m, 1). Even if you could somehow recalculate the height of the matrix at each loop iteration, you would have the problem that you're shifting rows up as you delete them, so would be skipping a row each time you delete one.
In any case, loops are not needed. Learn to operate on the whole matrix at once:
m = importdata(filename);
m(:, 1) = [];
m(mean(m, 2) < 50, :) = []; %delete all rows whose mean is less than 50
Also, I'm not sure why you have a continue in your loop. A continue as the last statement in a loop does absolutely nothing.