I have been filtering data in Octave but getting error
index out of bounds: value 741 out of bound 740'
X = 892x2 matrix and 2nd Column contains some value = 0. I want all rows to be removed having 0 as value.
Here is my Code
train_set = csvread('train.csv');
X = [train_set(:,3),train_set(:,7)];
y = train_set(:,2);
m = size(X,1);
for i=1:m,
if train_set(i,7) == 0,
X(i,:)=[];
endif
end
You are modifying X during the loop. This means that while initially it has 892 rows, after the first zero you reduce it to 891 rows, then to 890, etc etc.
At some point you've reduced it to 740 rows; and at some point after that, i
in the loop reaches the number 741.
This leads to the following kind of instruction: X(741, :) = []
Octave dutifully tells you that X doesn't have a 741st row anymore.
Instead, what you could do is mark the rows you want to delete, and delete them after the loop with a single command. Or better yet, without a foor loop, you can do:
Condition = train_set(:,7) == 0; % find all rows you want to delete
X( Condition, : ) = []; % Use logical indexing on rows to delete them.