필터 지우기
필터 지우기

Counting the number of elements in a network for ij matrix.

조회 수: 2 (최근 30일)
Niraj Bal Tamang
Niraj Bal Tamang 2022년 1월 23일
답변: Aditya 2023년 12월 22일
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
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.
Niraj Bal Tamang
Niraj Bal Tamang 2022년 1월 23일
Oh. I didn't notice that. Sorry for that. I have uploaded the sample data with additional data with From_Node and TO_Node indicating the starting point and ending or meeting point ID i.e. Each line in the network starts from From_node and ends at TO_NODE. One clue can be the fact that the TO_node of upper line can be the From_node of the lower line. I am trying to create a matrix like in the table where i can have the number of elements for each combination in the sample data. Thank you once again.

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

답변 (1개)

Aditya
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!

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by