MATLAB Answers

plot a line between any two neighbour points or more

4 views (last 30 days)
Hisham
Hisham on 26 Jan 2012
I need a small helps to complete my code. I have the following matrix: U3=[0 0 1 0 0 0 0 0; 0 0 1 0 0 0 0 0; 0 0 0 1 0 0 0 0; 0 0 1 1 1 0 0 0; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0]
and I want to plot a line between any two or more neighbours that have a 1 in its position (elements). I am trying my best to do the best. the steps that I have done them are:
[r,c]=find(U3==1) % find the row and column for each 1 value
neighB = [r-1 c; r+1 c; r c-1; r c+1] %find the neighbours of each 1 element
IDX = any(neighB < 1,2) | neighB(:,1) > size(U3,1) | neighB(:,2) > size(U3,2); % Exclude those out of matrix boundaries
then I sopped and looking for your helps...please. This is what I reached, may be you have another way better than me such as Island file>>
Note: My friend wrote this code to me but the code needs to take long-time and will effect on the process of the computer when I have a huge matrix. Here is his code:
[r,c]=size(U3);
figure
for cIdx=1:c, y=find(U3(:,cIdx)==1); %to find the ones in columns
if length(y)>1 % if there is one in one position at least
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

  0 Comments

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!