已知a1,为什么最后输出的介数中心性节点编号和坐标轴与前面代码输出的不一样,怎么改:degree = sum(a1~=0, 2); % 计算每个节点的度数 [~, idx] = sort(degree, 'descend'); % 按度数大小排序 x1_new = x1(idx); y1_new = y1(idx); z1_new = z1(idx); % 输出每个节点的坐标及编号 fid = fopen('node_coordinates.txt', 'w'); for i = 1:N fprintf(fid, 'Node %d: (%f,%f,%f)\n', idx(i), x1_new(i), y1_new(i), z1_new(i)); end fclose(fid); % 读取节点编号和坐标信息 fid = fopen('node_coordinates.txt'); C = textscan(fid, 'Node %d: (%f,%f,%f)'); fclose(fid); nodes = [C{2}, C{3}, C{4}]; node_ids =C{1}; node_pos = nodes(:,1:2); [BC1_sorted, BC1_idx] = sort(BC1, 'descend'); % 将介数中心性从高到低排序并记录排序后的索引 top5_idx = BC1_idx(1:5); % 取前5个节点的索引 for i = 1:5 node_id = top5_idx(i); node_bc = BC1(node_id); node_x = node_pos(node_id,1); node_y = node_pos(node_id,2); fprintf('节点 %d,介数中心性为 %f,坐标为 (%f,%f)\n', node_id, node_bc, node_x, node_y); end
时间: 2023-09-12 21:13:37 浏览: 36
在排序节点度数之后,你需要使用相同的排序索引 idx 来排序节点坐标和节点编号,以便保持它们的对应关系。修改后的代码如下:
```
degree = sum(a1~=0, 2); % 计算每个节点的度数
[~, idx] = sort(degree, 'descend'); % 按度数大小排序
x1_new = x1(idx);
y1_new = y1(idx);
z1_new = z1(idx);
% 输出每个节点的坐标及编号
fid = fopen('node_coordinates.txt', 'w');
for i = 1:N
fprintf(fid, 'Node %d: (%f,%f,%f)\n', idx(i), x1_new(i), y1_new(i), z1_new(i));
end
fclose(fid);
% 读取节点编号和坐标信息
fid = fopen('node_coordinates.txt');
C = textscan(fid, 'Node %d: (%f,%f,%f)');
fclose(fid);
nodes = [C{2}, C{3}, C{4}];
node_ids = C{1};
node_pos = nodes(:,1:2);
[BC1_sorted, BC1_idx] = sort(BC1, 'descend'); % 将介数中心性从高到低排序并记录排序后的索引
top5_idx = BC1_idx(1:5); % 取前5个节点的索引
for i = 1:5
node_id = idx(top5_idx(i)); % 使用排序后的索引来获取节点编号
node_bc = BC1(node_id);
node_x = node_pos(top5_idx(i), 1); % 使用未排序的索引来获取节点坐标
node_y = node_pos(top5_idx(i), 2);
fprintf('节点 %d,介数中心性为 %f,坐标为 (%f,%f)\n', node_id, node_bc, node_x, node_y);
end
```