필터 지우기
필터 지우기

How to convert the X & Y value in a matrix format?

조회 수: 3 (최근 30일)
Asyran Abdullah
Asyran Abdullah 2018년 9월 26일
댓글: Asyran Abdullah 2018년 9월 26일
Hi, Given X and Y as follow:
area_length = 100;
area_width = area_length ;
x = round((area_length - 1) * (rand - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand - 0) / (1 - 0) + 1);
How to convert the X & Y value into matrix format? Because i want to add a weights for make i possible to perform a shortest path.
x= ;
y= ;
weights = rand ;%-ok
G = graph(x,y,weights);
plot(G,'EdgeLabel',G.Edges.Weight);
Thanks for your help
  댓글 수: 6
KSSV
KSSV 2018년 9월 26일
YOu need to have those points to get the shortest path........do you have those points?
Asyran Abdullah
Asyran Abdullah 2018년 9월 26일
Yes, this is the point:
x = round((area_length - 1) * (rand - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand - 0) / (1 - 0) + 1);
x&y show the coordinate of each nodes, and the value will repeatedly change. However i don't know how to change the value into matrix in order to find a shortest path.

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 26일
편집: Walter Roberson 2018년 9월 26일
Your x and y calculated like the above define the coordinates of a single point. To do shortest path, you need multiple points.
I suggest,
N = 50; %nodes
cutoff = 25;
area_length = 100;
area_width = area_length ;
x = round((area_length - 1) * (rand(N,1) - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand(N,1) - 0) / (1 - 0) + 1);
A = squareform( pdist([x, y]) );
A(A > cutoff) = 0;
%and do any other tweaking for the A matrix
G = digraph(A);
plot(G, 'XData', x, 'YData', y);
and you can do shortest path calculations on G
With the above code, you could also use G = graph(A) to create undirected graphs, but if you do so then you would not be able to have asymmetric distance matrix. In the real world, there are cases where the transmit and receive path might not be the same.
  댓글 수: 1
Asyran Abdullah
Asyran Abdullah 2018년 9월 26일
Thanks Sir, it works!
N = 45; %nodes
cutoff = 25;
area_length = 100;
area_width = area_length ;
x = round((area_length - 1) * (rand(N,1) - 0) / (1 - 0) + 1);
y = round((area_width - 1) * (rand(N,1) - 0) / (1 - 0) + 1);
A = squareform( pdist([x, y]) );
A(A > cutoff) = 0;
%and do any other tweaking for the A matrix
G = graph(A );
G.Edges.W3 = randi(10, numedges(G) , 1); %laluan
G.Edges.Weight = G.Edges.W3; %weight
h = plot(G, 'XData', x, 'YData', y, 'EdgeLabel',G.Edges.Weight);
axis square;
grid on
G = rmedge(G, 1:numnodes(G), 1:numnodes(G));
title('Finding Shortest Path on Random Node');
path = shortestpath(G,1,45);
highlight(h,path,'NodeColor','g','EdgeColor','g')

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

추가 답변 (1개)

KSSV
KSSV 2018년 9월 26일
Simple rand..gives you a single number....try rand(N,1) with N as desired number of points.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by