How to map points in Vector A with many points in vector B?

Hi,
I have two row vectors with the same size.
A = randi(100,1,100); % Elements of A represent cities
B = randi([101, 200],1,100); % Elements of B represent some other cities
Is it possible to map each element in A with many elements in B ? For example, I want to connect A (i) with, let's say, three cities in B. Where i is a city in vector A.
Is it doable under some constraints?
Thanks!

4 comentarios

What do you want to do with this information? Do you want a graph object that would let you calculate distances between cities as well as shortest paths? Do you want to be able to look up which cities are connected with which other cities? Are you looking to try to populate a drop-down in an app with A and B to let users select two cities?
Knowing how you want to use this data may guide the best data structure to use to store the data.
Rayan Glus
Rayan Glus el 14 de Mzo. de 2021
Editada: Rayan Glus el 15 de Mzo. de 2021
Thanks for your reply, sir!
So, I've two grpahs where the nodes of both graphs represent cities and I want to study the interdependence of them. I'm taking an undergrad course on graph theory this spring so I don't have a specific idea in my mind, I'm still learning the basics.
Thanks again.
Hi Rayan,
For the case of one to many structure or mapping, the two networks or graphs cannot have the same number of nodes. Otherwise, half the number of nodes in one of the graphs would be considered autonomous nodes, that is, independent nodes.
So, if graph G has 8 nodes, graph H should have c * numnodes(G) nodes, where c is a positive integer. Let's assume that c = 2, then numnodes(H) = 16. Another constraint would be that each node i in G should be mapped to the same number of nodes in H. In our example, each node i in G should be mapped to two nodes in H.
The staff at Mathworks developed an amazing toolbox for graph theory, so you might want to check this page to help you have a better understanding.
Thanks for your nice explanation, Waseem.
So, I've been trying to map between A and B based on your explanation
A = 1:8;
B = 9:24;
But nothing works. I'm getting lots of errors. How should AB look like? a matrix?

Iniciar sesión para comentar.

Respuestas (1)

You can do this using an adjacency matrix:
A = [1 1 0; 0 0 1; 1 0 1];
D = digraph(A);
D.Nodes.Name = ["Boston"; "New York"; "Los Angeles"]
D =
digraph with properties: Edges: [5×2 table] Nodes: [3×1 table]
plot(D)
Or you could build an empty graph or digraph and use addedges and addnodes to built it up incrementally.
D2 = digraph;
D2 = addedge(D2, "Boston", "Boston");
D2 = addedge(D2, "Boston", "New York");
D2 = addedge(D2, "New York", "Los Angeles");
D2 = addedge(D2, "Los Angeles", "Boston");
D2 = addedge(D2, "Los Angeles", "Los Angeles")
D2 =
digraph with properties: Edges: [5×1 table] Nodes: [3×1 table]
plot(D2)

1 comentario

Thank you so much, Steven!
I tried your code while I was in the lab today, and it's just amzing.
But what I was looking for is how can I map the nodes of the digraph D you created with another digraph E's nodes. Is it via another adjacency Matrix?
A = [0 1 1; 1 0 1; 1 1 0];
E = digraph(A);
E.Nodes.Name = {'Las Vegas'; 'Miami'; 'Dallas'}
plot(E)
Thanks again.

Iniciar sesión para comentar.

Categorías

Más información sobre Graph and Network Algorithms en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Mzo. de 2021

Comentada:

el 18 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by