필터 지우기
필터 지우기

Count all unique elements in a 3d matrix

조회 수: 10 (최근 30일)
Vishal
Vishal 2013년 8월 8일
Hi all,
I have created a 3-d matrix randworld and would like to list the unique elements, count the number of unique elements and count the size of the unique elements for randworld(:,:,1), randworld(:,:,2) and randomworld(:,:,3). e.g. 4, 7, 13 is one unique element in this matrix with size 3.
randworld(:,:,1) =
4 4 3 11 11
4 9 3 9 10
2 7 9 3 9
7 6 6 9 3
4 1 15 15 13
randworld(:,:,2) =
7 7 6 3 3
7 11 6 6 10
11 4 11 9 6
4 15 15 6 9
1 8 5 5 5
randworld(:,:,3) =
13 13 9 4 4
13 11 9 3 6
13 4 11 12 3
4 1 1 3 12
11 5 15 15 15
Any help will be appreciated.
Thanks, Vishal
  댓글 수: 3
Jan
Jan 2013년 8월 8일
I do not understand: "4, 7, 13 is one unique element in this matrix with size 3." I see 9 elements of the value 4.
Could you provide a real example for the outputs for a [3x3x3] array?
Vishal
Vishal 2013년 8월 8일
Sorry, I'll try and make it my question more clear. In the 3-d matrix randworld as listed below, I am trying to find the following:
randworld(:,:,1) =
4 4 3
4 9 3
2 7 9
randworld(:,:,2) =
7 7 6
7 11 6
11 4 11
randworld(:,:,3) =
13 13 9
13 11 9
13 4 11
1. list the unique elements-
4,7,13
2,11,13
9,11,11
7,4,4
3,6,9
9,11,11
2. count the number of unique elements
4,7,13 (count is 3) & 2,11,13 (count is 1) & 9,11,11 (count is 1) & 7,4,4 (count is 1) & 3,6,9 (count is 2)& 9,11,11 (count is 1)

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 8일
a1=randworld(:,:,1)
a2=randworld(:,:,2)
a3=randworld(:,:,3)
v=cell2mat(arrayfun(@(x1,x2,x3) [x1 x2 x3],a1(:),a2(:),a3(:),'un',0));
[a,b,c]=unique(v,'rows','stable')
idx=histc(c,(1:size(a,1))')
% a represent unique vectors
% idx represent the repetition of each vector
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 8일
Or simply
a1=randworld(:,:,1);
a2=randworld(:,:,2);
a3=randworld(:,:,3);
v1=[a1(:) a2(:) a3(:)];
[a,b,c]=unique(v,'rows','stable')
idx=histc(c,(1:size(a,1))')
Vishal
Vishal 2013년 8월 12일
Yes, that works fine as well. Just a small typo. In line 4 replace v with v1. Thanks mate.
a1=randworld(:,:,1); a2=randworld(:,:,2); a3=randworld(:,:,3); v1=[a1(:) a2(:) a3(:)]; [a,b,c]=unique(v1,'rows','stable') idx=histc(c,(1:size(a,1))')

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

추가 답변 (2개)

Jan
Jan 2013년 8월 8일
It sounds like a basic task for unique and histc. But currently I do not understand the needs exactly.
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 8일
I think, he wants to form vectors from each chanel:
vector1= [a(1,1,1) a(1,1,2) a(1,1,3)]
vector2=[a(1,2,1) a(1,2,2) a(1,2,3)]
and so on

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


dpb
dpb 2013년 8월 8일
Permutation on above...
rpr=permute(r,[1 3 2]);
r2d=rpr(:,:,1);for i=2:size(rpr,3),r2d=[r2d; rpr(:,:,i)];end
u=unique(r2d,'rows','stable');
  댓글 수: 2
Vishal
Vishal 2013년 8월 12일
This solutions works fine if the task is to just list the unique rows. Thanks
dpb
dpb 2013년 8월 12일
Well, one presumes one would use the u as in the previous to determine the rest having found them. Didn't see any point in repeating that.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by