필터 지우기
필터 지우기

Finding similar elements in a single matrix

조회 수: 4 (최근 30일)
Md. Nazrul Islam Siddique
Md. Nazrul Islam Siddique 2022년 12월 2일
댓글: Star Strider 2022년 12월 2일
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
Walter Roberson
Walter Roberson 2022년 12월 2일
So you just need to know the total number of non-zero elements? You can use nnz for that.
Md. Nazrul Islam Siddique
Md. Nazrul Islam Siddique 2022년 12월 2일
That's easy. what about the non zero elements between two metrices?

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

채택된 답변

Star Strider
Star Strider 2022년 12월 2일
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
Md. Nazrul Islam Siddique
Md. Nazrul Islam Siddique 2022년 12월 2일
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.
Star Strider
Star Strider 2022년 12월 2일
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.
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Preprocessing Data에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by