n1=20; n2=20; file = fopen('liangcengjiedian.txt', 'r'); if file ~= -1 n_gateway = 0; gateway1 = []; gateway2 = []; while ~feof(file) line = fgetl(file); if ~isempty(line) data = sscanf(line, '(%f,%f,%f) to (%f,%f,%f)'); if length(data) == 6 n_gateway = n_gateway + 1; x1 = data(1); y1 = data(2); z1 = data(3); x2 = data(4); y2 = data(5); z2 = data(6); gateway1 = [gateway1; x1, y1, z1]; gateway2 = [gateway2; x2, y2, z2]; end end end fclose(file); else disp('Error: file not found or could not be opened.'); end disp(gateway1); disp(gateway2); % 使用randperm函数随机选择n_gateway个网关 n_gateway = min(n_gateway, size(gateway1, 1)); if n_gateway > 0 idx = randperm(size(gateway1, 1), n_gateway); gateway1 = gateway1(idx, :); gateway2 = gateway2(idx, :); end gateway1和2中有些节点坐标是重复的,因为第一层一个节点可能与另一层两个节点有连接边,所以在提取网关节点时该怎么修改代码
时间: 2024-01-04 20:04:30 浏览: 70
可以在提取网关节点时,使用unique函数对gateway1和gateway2进行去重操作,保证每个节点只出现一次。修改后的代码如下:
```
n1=20;
n2=20;
file = fopen('liangcengjiedian.txt', 'r');
if file ~= -1
n_gateway = 0;
gateway1 = [];
gateway2 = [];
while ~feof(file)
line = fgetl(file);
if ~isempty(line)
data = sscanf(line, '(%f,%f,%f) to (%f,%f,%f)');
if length(data) == 6
n_gateway = n_gateway + 1;
x1 = data(1);
y1 = data(2);
z1 = data(3);
x2 = data(4);
y2 = data(5);
z2 = data(6);
gateway1 = [gateway1; x1, y1, z1];
gateway2 = [gateway2; x2, y2, z2];
end
end
end
fclose(file);
else
disp('Error: file not found or could not be opened.');
end
% 对gateway1和gateway2进行去重操作
[gateway1, ia1] = unique(gateway1, 'rows');
gateway2 = gateway2(ia1, :);
[gateway2, ia2] = unique(gateway2, 'rows');
gateway1 = gateway1(ia2, :);
disp(gateway1);
disp(gateway2);
% 使用randperm函数随机选择n_gateway个网关
n_gateway = min(n_gateway, size(gateway1, 1));
if n_gateway > 0
idx = randperm(size(gateway1, 1), n_gateway);
gateway1 = gateway1(idx, :);
gateway2 = gateway2(idx, :);
end
```
阅读全文