为什么会出现无法执行赋值,因为左侧的大小为 1-by-3,右侧的大小为 0-by-0:% 读取节点坐标 fid = fopen('node_coordinates555.txt', 'r'); node_coords = zeros(length(a1), 3); for i=1:length(a1) coords = fscanf(fid, '%f', 3); node_coords(i,:) = coords'; end fclose(fid); % 计算网络连通介数中心性 n = length(a1); bc = zeros(n, 1); for s=1:n % 初始化 queue = []; dist = -1 * ones(n, 1); sigma = zeros(n, 1); sigma(s) = 1; dist(s) = 0; queue(end+1) = s; % BFS while ~isempty(queue) v = queue(1); queue = queue(2:end); for w=find(a1(v,:)) if dist(w) < 0 queue(end+1) = w; dist(w) = dist(v) + 1; end if dist(w) == dist(v) + 1 sigma(w) = sigma(w) + sigma(v); end end end % 计算介数中心性 delta = zeros(n, 1); while ~isempty(queue) w = queue(end); queue = queue(1:end-1); for v=find(a1(:,w))' if dist(v) == dist(w) - 1 delta(v) = delta(v) + (sigma(v) / sigma(w)) * (1 + delta(w)); end end if w ~= s bc(w) = bc(w) + delta(w) * f1(s,w); end end end % 找出前十个重要节点 [~, idx] = sort(bc, 'descend'); disp('Top 10 important nodes:'); for i=1:10 fprintf('%d. Node %d, (%.2f, %.2f, %.2f)\n', i, idx(i), node_coords(idx(i),:)); end
时间: 2023-11-13 08:02:52 浏览: 65
这个错误是因为在读取节点坐标之前,你已经声明了一个名为node_coords的大小为1-by-3的矩阵,但是在读取节点坐标时,你尝试将值赋给node_coords的所有行,但是这样做会导致左边的大小为1-by-3,而右边的大小为0-by-0,从而导致无法执行赋值的错误。
解决这个问题的方法是在读取节点坐标之前,将node_coords矩阵初始化为一个大小为length(a1)-by-3的零矩阵,以便在读取节点坐标时,能够正确地赋值给矩阵的每一行。修改后的代码如下:
```
fid = fopen('node_coordinates555.txt', 'r');
node_coords = zeros(length(a1), 3);
for i=1:length(a1)
coords = fscanf(fid, '%f', 3);
node_coords(i,:) = coords';
end
fclose(fid);
% 计算网络连通介数中心性
n = length(a1);
bc = zeros(n, 1);
for s=1:n
% 初始化
queue = [];
dist = -1 * ones(n, 1);
sigma = zeros(n, 1);
sigma(s) = 1;
dist(s) = 0;
queue(end+1) = s;
% BFS
while ~isempty(queue)
v = queue(1);
queue = queue(2:end);
for w=find(a1(v,:))
if dist(w) < 0
queue(end+1) = w;
dist(w) = dist(v) + 1;
end
if dist(w) == dist(v) + 1
sigma(w) = sigma(w) + sigma(v);
end
end
end
% 计算介数中心性
delta = zeros(n, 1);
while ~isempty(queue)
w = queue(end);
queue = queue(1:end-1);
for v=find(a1(:,w))'
if dist(v) == dist(w) - 1
delta(v) = delta(v) + (sigma(v) / sigma(w)) * (1 + delta(w));
end
end
if w ~= s
bc(w) = bc(w) + delta(w) * f1(s,w);
end
end
end
% 找出前十个重要节点
[~, idx] = sort(bc, 'descend');
disp('Top 10 important nodes:');
for i=1:10
fprintf('%d. Node %d, (%.2f, %.2f, %.2f)\n', i, idx(i), node_coords(idx(i),:));
end
```
阅读全文