Finding similar elements in a single matrix

I have one matrix,
x = [
2 0 0
3 3 0
4 4 0
5 5 5
6 6 6]
How can I find the common elements between columns in the matrix and count them? for example, here 3 and 4 repeated two times.

4 Comments

SUppose that row 3 column 3 were 2. Should that be considered a match to row 1 column 1? Or is it only a match if at least two columns of the same row are equal?
Should the 0 be counted as matches in row 1?
What should the output be? If there were another row at the end that was [3 0 3] for example, then should the overall output summarize that 3 occurred 4 times? But if so then should the 5 5 5 count as 3 because there are 3 elements? Should it count as 3 because columns (1,2) -> 1 match, columns (1,3) -> 1 match, colums (2,3) -> 1 match, total 3? So if there were 4 rows all identical items the match count for the row should be 6?
Yes. I need to match one item at a time. Here, 3 and 4 repeated 2 times and 5 and 6 repeated 3 times. Number 2 is repeated only 1. So the total count is 11. I don't want to count the number 0.
So you just need to know the total number of non-zero elements? You can use nnz for that.
That's easy. what about the non zero elements between two metrices?

Sign in to comment.

 Accepted Answer

One approach using accumarray
x = [2 0 0
3 3 0
4 4 0
5 5 5
6 6 6];
[Aunique,~,idx] = unique(x(:));
Counts = accumarray(idx, 1);
Result = table(Aunique, Counts)
Result = 6×2 table
Aunique Counts _______ ______ 0 4 2 1 3 2 4 2 5 3 6 3
.

9 Comments

If I don't want to count the number 0?
x = [2 0 0
3 3 0
4 4 0
5 5 5
6 6 6];
xnz = x(x~=0);
[Aunique,~,idx] = unique(xnz);
Counts = accumarray(idx, 1);
Result = table(Aunique, Counts)
Result = 5×2 table
Aunique Counts _______ ______ 2 1 3 2 4 2 5 3 6 3
But do you want to find matching elements, or do you want to find rows in which columns match each other?
I want to find the matching elements. The above example will do. Is there any other approach?
If I want to match two matrices for the common elements, what will be the approach?
for example:
x = [
1 2 3
4 5 6]
y = [1 5 7
2 5 8].
x = [
1 2 3
4 5 6]
x = 2×3
1 2 3 4 5 6
y = [1 5 7
2 5 8]
y = 2×3
1 5 7 2 5 8
intersect(x, y)
ans = 3×1
1 2 5
I want to know the total number which is repeated. For example, 1 and 2 repeated two times, 5 repeated three times. Other numbers are repeated only one. The total count is: 12.
Just sum the ‘Count’ results —
x = [1 2 3
4 5 6];
y = [1 5 7
2 5 8];
xy = [x; y]
xy = 4×3
1 2 3 4 5 6 1 5 7 2 5 8
xy = xy(xy~=0);
[XYunique,~,idx] = unique(xy(:));
Counts = accumarray(idx, 1);
Result = table(XYunique, Counts)
Result = 8×2 table
XYunique Counts ________ ______ 1 2 2 2 3 1 4 1 5 3 6 1 7 1 8 1
TotalCounts = sum(Counts)
TotalCounts = 12
If the matrices are not conformable to concatenation (they are conformable here), then reshape both to be column vectors using the column vector operator ‘(:)’ and then vertically concatenate those. Then use unique and accumarray.
.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!