conditional deletion of riws

2 views (last 30 days)
Sherin
Sherin on 9 Nov 2014
Commented: Image Analyst on 6 Dec 2014
Dear Matlab users, I am writing a matlab script to delete some rows based on some condition. I want to look for some symmetric matrix around a diagonal line of "ones"
(1). I want, in each row, to look for elements which are > 0.7 but less than 1.0.
if there is a value in row "i" and column "j" which satisfies this condition. There should be cell with the same value in row "j" and column "i".
I want to compare these two rows (for each case the conditions are satisfied) and delete the one that has the larger value in the last column for each.
I am writing the script shown below but it does not delete any line and I do not understand why. Would you please advice????? Regards Shireen
M = xlsread('test.xlsx');
[r,c]=size(M);
for i =1:r,
for j=1:c-1
if (M(i,j) >= 0.7) & (M(i,j)<0.1) % get row and column index of points > 0.7
M(i,c)
M(j,c)
if M(i,c)>M(j,c)
nM(i,:)=[ ] % delete the row with the larger "c" value
else
nM(j,:)=[ ]
end
end
end
end

Accepted Answer

Guillaume
Guillaume on 9 Nov 2014
It would be so much easier to read your code if you'd use the code formatting button: {} Code.
I see several issues with your code:
  1. You never define nM
  2. Your first if test is wrong. You're testing if a value is greater than 0.7 and smaller than 0.1 instead of 1.0. That condition will never be true.
  3. Once a row has been deleted your row index is out of sync with the actual rows of nM. For example let's say you delete row 4. the new row 4 is the original row 5, the new row 5 is the old row 6, etc. If you then decide to delete row 5, you're actually deleting the original row 6. You must only perform the deletion after the loop.
  4. You only need to iterate over the upper or lower diagonal of the matrix, since it's symmetric. It may be what you were trying to do, but the upper bound of the 2nd loop should have read i-1 not c-1.
I believe the following should work
[r, c] = size(M);
todelete = [];
for row = 1:r %I prefer more descriptive variable names than i
for col = 1:row-1 %I prefer more descriptive variable names than j
if M(row, col) >= 0.7 && m(row, col) < 1.0
if M(row, end) > M(col, end)
todelete = [todelete row];
else
todelete = [todelete col];
end
end
end
end
nM = M(setdiff(1:r, todelete), :);
  5 Comments
Guillaume
Guillaume on 3 Dec 2014
Shayma,
You would be better off starting a new question, as people don't tend look at questions marked as answered.
I don't really understand your question (but I've forgotten the details of the original question now), so you'll want to add an example as well.
Image Analyst
Image Analyst on 6 Dec 2014
Shayma's "Answer" moved here so he can Accept Guillaume's answer:
Dear Guillaume, Thank you very much for your answer. I am sorry for the typos in my post.
I wonder if you can explain what the command line shown below works?
"todelete = unique([r(deleterow) c(~deleterow)]);"
I have read the page of the unique command but I am not sure if I got the lines you are writing.
Many Thanks Shireen

Sign in to comment.

More Answers (1)

dpb
dpb on 9 Nov 2014
if (M(i,j)>=0.7) & (M(i,j)<0.1)
The above is impossible...you wrote "0.1" for "1.0" in the second comparison
  1 Comment
Sherin
Sherin on 11 Nov 2014
I am sorry, it was a typo! Thanks for the note.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!