MATLAB: Problem with intersect function

intersect

I am writing a matlab programm to import xls files from local and do some calculations. These xls files alwasys have four cloummns but different number of rows, each colummn from left to right is considered as a, b,c and d. And my code need to compare two adjacent rows, when they meet some conditions, put results in a matrix. I am using a nested loop to compare rows, and use intesect command to keep the order of the matrix and suppress repeated rows. But here is the problem, because intersect function will suppress repeated rows for each matrix before compare the value, so if there are repeated rows in the original data, my code will fail. Any advice? Thanks.
[vert,horz]=size(data);
for ii=1:vert-1;
for jj=ii+1:vert;
if data(ii,1)+data(jj,1)-data(ii,2)-data(jj,2)+data(ii,3)+data(jj,3)-data(ii,4)-data(jj,4)<10;
result=[data(ii,1),data(ii,2),data(ii,3),data(ii,4);data(jj,1),data(jj,2),data(jj,3),data(jj,4);result]
end
end
end
result=intersect(data,result,'stable','rows');

Best Answer

Perhaps you want:
index = ismember(data, result, 'stable', 'rows');
result = data(index, :); % [EDITED, Typo fixed]
This keeps the repetitions in the matrix data. Or do you want to keep the repetitions in the first version of result? Or both?
Perhaps it helps to post a short example.