Hello, I have question which i need to solve. if i got a matrix, how can i check if there is any equal rows in it ? without using loops,if,unique,for :\ thanks

回答 (3 件)

Guillaume
Guillaume 2015 年 12 月 2 日
編集済み: Guillaume 2015 年 12 月 2 日
Without using loops or unique, I don't think it's really possible. You can cheat by using set functions such as union, setdiff, setxor, intersect etc. but internally all of these have a call to unique. I say it's cheating because all you'd be doing is using the set function to call unique.
hasrepeatedrows = @(m) size(intersect(m, m, 'rows'), 1) ~= size(m, 1); %intersect(a,a) is the same as unique(a)
>>m = [1 2 3 ; 4 5 6; 1 2 3; 4 5 0];
>>hasrepeatedrows(m)
ans =
1
>>m = [1 2 3;4 5 6;7 8 9];
>>hasrepeatedrows(m)
ans =
0

2 件のコメント

Mira Najjar
Mira Najjar 2015 年 12 月 2 日
i cant use functions also :S i can only use, repmat, reshape,ones, zeros, double, char
Guillaume
Guillaume 2015 年 12 月 2 日
編集済み: Guillaume 2015 年 12 月 2 日
It's definitively not possible to do what you want with just repmat, reshape, ones, zeros, double or char (I don't see why you'd want to use that last one). All of these would need to know what size of array to generate, so you would at least also need size.
Even then, I don't see how these functions would help. At the base, the way to find duplicate rows is to loop over the rows and compare them to each other. unique does that for you. If you can' use unique you have to do it yourself. So it's loop or unique, you can't exclude both.

サインインしてコメントする。

dpb
dpb 2015 年 12 月 2 日
function isMult=ismultirow(x)
% return logical T if are identical multiple rows in input X array
isMult=size(unique(M,'rows'),1)<size(M,1)

1 件のコメント

dpb
dpb 2015 年 12 月 2 日
"without using loops,if,unique,for..."
I figure even Ruth, DiMaggio, Williams never hit 3 out of 4... :)
All, I'd also claim "loops" and "for" are redundant as well.

サインインしてコメントする。

dpb
dpb 2015 年 12 月 2 日
編集済み: dpb 2015 年 12 月 2 日
Well, there is a way w/o unique; I presumed the prohibition against unique originally wasn't really so; only to avoid a looping solution...
>> any(~any(diff(sortrows(M)),2))
ans =
1
>>
It does still use several functions, of course, and again to implement without those would revert to looping constructs. I agree I see no way to do multiple comparisons without some form of repetitive "one-to-others" operations.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2015 年 12 月 2 日

編集済み:

dpb
2015 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by