Find similar elements in a matrix

My question is that , I have a matrix, I want to know which element from which columns are similar, which number is the most repeated number for example
X=[1 2 3 3 3
45 7 4 4 4
70 8 5 5 5
88 9 11 11 11
170 205 13 13 13
172 220 14 23 24
194 222 24 24 41
196 224 41 152 67
200 539 62 183 68
250 540 67 184 71
251 1415 68 185 148
255 1426 71 187 151
]
for example here 3 (repeated in columns 3,4 and5) the same for 4, 5, 1, 13
and the most repeated numbers are 3,4,5,11, and 13

댓글 수: 2

Grzegorz Knor
Grzegorz Knor 2011년 9월 5일
I've question:
most repeated numbers are 3,4,5,11, and 13
or rather
most repeated numbers are 3,4,5,11, 13 and 24?
Niki
Niki 2011년 9월 5일
you are right
3,4,5,11, 13 and 24

댓글을 달려면 로그인하십시오.

 채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 9월 5일

0 개 추천

[h1 h2] =hist(X(:),unique(X));
h3 = unique(h1);
h3 = h3(end:-1:1);
for k=1:length(h3)
disp(['There is ' num2str(h3(k)) ': [ ' num2str(h2(h1==h3(k))') ' ]'])
end

댓글 수: 2

Grzegorz Knor
Grzegorz Knor 2011년 9월 5일
If you think that this answer solved your problem, please accept it :)
Niki
Niki 2011년 9월 5일
Grzegorz, It works , thanks , also could you please take a look at one another question that I have?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Daniel Shub
Daniel Shub 2011년 9월 5일

2 개 추천

Maybe something like:
[y, z] = hist(X(:), unique(X));
stem(z, y);

댓글 수: 1

Niki
Niki 2011년 9월 5일
Thanks Daniel, your comments always make a brilliant way,

댓글을 달려면 로그인하십시오.

Grzegorz Knor
Grzegorz Knor 2011년 9월 5일

0 개 추천

My suggestion:
[u, ~, n] = unique(X);
[h1 h2] =hist(n,unique(n));
u(h2(h1==max(h1)))

댓글 수: 3

Niki
Niki 2011년 9월 5일
Oh, at first with Unique you put the matrix in a column (n the values) and u is the correspond
using "hist" you found the repeated values (how many times they are repeated is saved in h2)
and the final command you found the maximum, sounds perfect :D
but please check it on this matrix
X=[1 2 3 3 3 6
46 7 4 4 4 38
71 8 5 5 5 87
89 9 11 11 11 426
171 206 13 13 13 0
173 221 14 24 25 0
195 223 25 25 42 0
197 225 42 153 68 0
201 540 63 184 69 0
251 541 68 185 72 0
252 1416 69 186 149 0
256 1427 72 188 152 0
257 1429 125 204 153 0
377 1435 137 224 154 0
378 0 145 227 165 0
940 0 149 232 176 0
941 0 150 237 183 0
1165 0 151 242 184 0
0 0 152 247 185 0
0 0 153 263 186 0
0 0 154 264 187 0
0 0 165 279 188 0
0 0 166 280 200 0
0 0 169 463 202 0
0 0 172 466 204 0
0 0 176 467 227 0
0 0 183 468 232 0
]
Grzegorz Knor
Grzegorz Knor 2011년 9월 5일
There is 45 zeros:
sum(X(:)==0)
Is this OK?
Niki
Niki 2011년 9월 5일
Yes, exactly
It is okay, but I would like to see the others as well, for example 45 times zero, then 3,4,5,11, 13 and 24

댓글을 달려면 로그인하십시오.

질문:

2011년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by