# if loop that is not working properly, row should be deleted based on several criterias but do not

1 Ansicht (letzte 30 Tage)
Locks am 13 Apr. 2013
hi,
I have a matrix with 8 columns and I would like to delete those rows where the value of column 8 exceeds 1.1 or is below 0.9 and apply a similar criteria on column 5, here is the respective code:
while i < size(data8, 1)
i = i + 1 ;
if(data8(i,8)>1.1)
data8(i,:) = [];
elseif(data8(i,8)<0.9)
data8(i,:) = [];
elseif(data8(i,5)<5*1/365)
data8(i,:) = [];
elseif(data8(i,5)>120*1/365)
data8(i,:) = [];
end
end
although the code is running, I get in the resulting matrix values in column 8 that exceeds 1.1 and are below 0.9, whan do I need to change?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Image Analyst am 13 Apr. 2013
Try it this way
rowsToDelete = data(:,8) > 1.1 | data(:,8) < 0.9;
data(rowsToDelete, :) = [];
Do the same for column #5. You could combine column 5 into the calculation of rowsToDelete if you want.
##### 2 KommentareKeine anzeigenKeine ausblenden
Locks am 13 Apr. 2013
I tried this:
i = 0 ;
while i < size(data8, 1)
i = i + 1 ;
rowsToDelete = data8(:,8) > 1.1 | data8(:,8) < 0.9 | data8(i,5)<5*1/365 | data8(i,5)>120*1/365;
data8(rowsToDelete, :) = [];
%if(data8(i,8)>1.1)
%data8(i,:) = [];
%elseif(data8(i,8)<0.9)
%data8(i,:) = [];
%elseif(data8(i,5)<5*1/365)
%data8(i,:) = [];
%elseif(data8(i,5)>120*1/365)
%data8(i,:) = [];
%end
end
but this gives me an empthy data8 matrix, which cannot be true, is there anything else which is incorrect?
What was the problem with the first code?
Image Analyst am 13 Apr. 2013
No. The code was all there was. The whole point of vectorizing it was to get rid of the while loop - and you put it back in. Don't use it. Try this, which I constructed to use sample data in the rang 0.8 - 1.2.
% Create array of numbers between 0.8 and 1.2.
data = .8 + .4 * rand(20,8)
% Display rows 8 and 5
data(:,8)
data(:,5)
% Find the rows to delete.
rowsToDelete8 = data(:,8) > 1.1 | data(:,8) < 0.9
rowsToDelete5 = data(:,5) > 1.1 | data(:,5) < 0.9
rowsToDelete = rowsToDelete8 & rowsToDelete5
% Go ahead and delete them.
data(rowsToDelete, :) = [];
% Display final data.
data

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Locks am 13 Apr. 2013
awesome, this is working. Could you shortly explain me what rowsToDelete is doing? I get just zeros and from what I know zero means false, but I don't understand the logic behind it.
What is the | operator doing? is that the same as &...?
Thanks vermy much for the help!
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Locks am 13 Apr. 2013
that means that this command:
rowsToDelete8 = data8(:,8) > 1.1 | data8(:,8) < 0.9;
data8(rowsToDelete8, :) = [];
each times delete the respective row then the the 8th colum is either above 1.1 or below 0.9?
Image Analyst am 13 Apr. 2013
Bearbeitet: Image Analyst am 13 Apr. 2013
That's correct. If we're done, then mark my answer (not yours) as Accepted.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by