Counting the number of elements in a network for ij matrix.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Voss
2022-1-23
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.
回答(1 个)
Aditya
2023-12-22
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!