# 3D matrix representation problem

1 view (last 30 days)
Hisham on 22 Aug 2012
Hello, I asked before similar to my question and some programmers helped me to solve my problem. Actually I thought that they solved my problem but unfortunately I detected one error in their code. Let me firstly explain the question to be clear to all of you. The question is how can I give each chain an individual number that can be distinguished from the other.? What do I mean by a chain? Answer: I will explain that after the matrix to be clear to all of you. The 3D Matrix is:
U3(:,:,1) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0
];
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
];
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 1
];
Now I will explain the meaning of the chain(s):
The first chain is: U3(3,2,1) and U3(3,2,2)and U3(4,2,2) and U3(5,2,2)and U3(3,2,3)
The second chain is: U3(3,5,1) and U3(4,5,1) and U3(3,5,2)
The third chain is: U3(4,5,3) and U3(5,5,3)
How can I give each chain a similar individual number?
(i.e).
U3(3,2,1) and U3(3,2,2)and U3(4,2,2) and U3(5,2,2)and U3(3,2,3)=2
U3(3,5,1) and U3(4,5,1) and U3(3,5,2)=3
U3(4,5,3) and U3(5,5,3)=4
One of the suggested solutions is to use :
CC = bwconncomp(U3);
labelmatrix(CC)
But this function is not valid for my case because it gives the first chain individual number (which is fine), and combined the second and third chains in one number (which is not fine)
See the output (which is wrong not as I want).
CC = bwconncomp(U3); labelmatrix(CC)
ans(:,:,1) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 2
0 0 0 0 2
0 0 0 0 0
ans(:,:,2) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 2
0 1 0 0 0
0 1 0 0 0
ans(:,:,3) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 2
0 0 0 0 2
Note: I want to check for the connectivity in 3D " any of the 6-connected neighborhood"

José-Luis on 22 Aug 2012
conn = zeros(3,3,3);
conn(2,2,:) = 1;
conn(:,2,2) = 1;
conn(2,:,2) = 1;
CC = bwconncomp(U3,conn);
Note that there might be other connectivity patterns that fulfill your conditions.
Cheers!