MATLAB: Conditional deletion of riws

conditionaldelete row

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

Best Answer

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), :);