MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

develop plotting from 2D to 3D

1 view (last 30 days)
Hisham
Hisham on 24 Aug 2012
Hi,
If I have a matrix in 3D and it contains only either 1 or 0. If any 1's neighbour either in X direction, Y direction or Z direction, I want to represent it these (1)s as a line. Actually, one of friends programmed it for 2D for me. I want to know how can I make it applicable for 3D.
Here is his code for 2D only.
U3 = [
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
1 , 1 , 0 , 0 , 1;
1 , 0 , 0 , 0 , 0
]
[r, c, v]=size(U3);
figure
for cIdx=1:c,
y=find(U3(:,cIdx)==1);
if length(y)>1
y=(r+1)-y;
x=ones(size(y))*cIdx;
for idx=1:length(x)-1
if abs( y(idx) - y(idx+1))<=1
line(x(idx:idx+1),y(idx:idx+1),'LineWidth',4)
end
end
hold on
end
end
for rIdx=1:r,
x=find(U3(rIdx,:)==1);
if length(x)>1
y=(r+1)-ones(size(x))*rIdx;
for idx=1:length(x)-1
if abs( x(idx) - x(idx+1))<=1
line(x(idx:idx+1),y(idx:idx+1),'LineWidth',4)
end
end
hold on
end
end
In my case, I want to do it for 3D. Could you please advice me?
Here is a 3D matrix:
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 , 0;
0 , 1 , 0 , 0 , 1;
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
]

  2 Comments

Doug Hull
Doug Hull on 24 Aug 2012
people will not write this code for you. What specific questions do you have? Edit your question with specifics, do not add a comment or answer.
Hisham
Hisham on 24 Aug 2012
Thanks Hull. I gave you the picture and the code to keep everything clear to any one that might know some function to do it. Actually there is a function called "Plot a 3D array using patch" but it is not doing similar to mine. The thing that I want to know either add some edits on my code or advice me some other function that can help me. As you know the Matlab has too many function which is impossible for an elementary one to cover all of them.
Any way, my issue is how can I edit the above code to be used for 3D. In more specific, how can edit these lines:
y=(r+1)-y;
x=ones(size(y))*cIdx;

Answers (0)

Community Treasure Hunt

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

Start Hunting!