Counting the number of elements in a network for ij matrix.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a network with order number assigned for each link. I want to count the number of the link such that i get the values for the number of 1s joining 2, 1s joining 3, 1s joining 4 separately. Similarly i want to calculate the count for 2s joining 3, 4. only condition is that the value direction can't be decreasing i.e. 2s to 1, 3s to 2. Finally, i want to create a matrix based upon the count. I am uploading the sample data if anyone want to give it a try in Matlab. I am also attaching the pics to clarify what i am trying to do. Thanks a lot.
2 comentarios
Voss
el 23 de En. de 2022
The .xlsx file contains a column of unique HydroIDs and a column of associated Orders (1,1,1,2,1, ...). How should the HydroIDs be interpreted to ascertain which 1s are connected to which 2s, etc.? Or is that part of your question? Sorry, I may be missing something, but it's not clear how the data in the file describes connections in a network.
Respuestas (1)
Aditya
el 22 de Dic. de 2023
Hi Niraj,
I understand that you want to create a matrix that counts the number of links with a particular order that are connected to nodes with a higher order.
Here's a MATLAB script that could perform the task you've described using the sample data you provided.
% Read the data from a xlsx file into a table
data = readtable('sample.xlsx');
% Find the unique orders in the network
unique_orders = unique(data.Order);
% Sort the unique orders to ensure they are in ascending order
unique_orders = sort(unique_orders);
% Initialize the matrix to store the counts of links between orders
order_matrix = zeros(length(unique_orders));
% Loop over the data to count the links
for i = 1:height(data)
from_order = data.Order(i);
to_node = data.TO_NODE(i);
% Find all the orders of the 'to_node'
to_order_rows = data(data.FROM_NODE == to_node, :);
% Iterate over each 'to_order_row' and increment the count if the order is increasing
for j = 1:size(to_order_rows, 1)
to_order = to_order_rows.Order(j);
if to_order > from_order
from_index = find(unique_orders == from_order);
to_index = find(unique_orders == to_order);
order_matrix(from_index, to_index) = order_matrix(from_index, to_index) + 1;
end
end
end
% Convert the matrix to a table for better readability
order_table = array2table(order_matrix, 'RowNames', string(unique_orders), 'VariableNames', string(unique_orders));
% Display the resulting table
disp(order_table);
This script iterates over all rows where the “FROM_NODE” matches the “TO_NODE” from the current row in the loop. It increments the count in the “order_matrix” only if the order is increasing, as per your requirement. The resulting “order_table” will have row and column names that correspond to the unique orders, making it more representable and easier to understand.
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!