Problem with intersect function?

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');

1 个评论

Jan
Jan 2017-10-4
编辑:Jan 2017-10-4
I have edited you code to format the code. Please use the "{} Code" button the next time - thank you.
As far as I understand, it does not matter, that the data are coming from an Excel file and the pre-sorting in the nested loop is not a problem also. So all you are asking for is using intersect with keeping repeated rows of the inputs? But what do you expect as output then? This is not uniquely defined.

请先登录,再进行评论。

 采纳的回答

Jan
Jan 2017-10-4
编辑:Jan 2017-10-4
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.

4 个评论

The expected result is to keep the rows that meet the condition. So the result should also be a n by 4 matrix but with same order as original data and repeated rows in original data should also be kept. Original data is not sorted.
Now I've tried index=ismember(data,result,'rows') command and I got a logical column vector. But I don't know how to 'import' this index to my result :(
There was a typo in my code. You need data(index, :), not data(ismember, :). I should drink a coffee before answering.
Again: A short example would help to understand, what you want. Follo9wing your description, the two parts of the result:
p1 = result(:, 1:2)
p2 = result(:, 3:4)
would be identical. Is this really wanted and useful? If so:
index = ismember(data, result, 'stable', 'rows');
result = [data(index, :), data(index, :)];
Or do you want to keep all rows, which appear in the other matrix? But then it is not guaranteed, that the number of rows is the same:
data = [1,2,3; 2,3,4; 1,2,3];
result = [1,2,3; 1,2,3; 1,2,3; 4,5,6];
What is the wanted result now?
I sorted it out by reshape the data matrix to column vector and match it to the index then reshape it back :) Your answer hinted me to use ismember function :)Thank you very much.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by