% 找出同时出现在四个结果中的节点坐标 common_nodes = intersect(intersect(nodes1, nodes2, 'rows'), nodes3, 'rows'); fprintf('同时出现在四个结果中的节点坐标:\n'); disp(common_nodes);为什么没有结果输出
时间: 2023-12-05 14:03:25 浏览: 16
如果没有输出结果,可能是因为 `common_nodes` 中没有任何元素。这可能是由于以下几种原因之一造成的:
- `nodes1`,`nodes2`,`nodes3` 中没有任何一个包含与其他两个相同的节点。这意味着没有节点同时出现在这三个结果中。
- `nodes1`,`nodes2`,`nodes3` 中有一些节点共同出现,但它们的坐标值非常接近,以至于在计算交集时被视为不同的节点。这可能是由于浮点精度问题造成的。
你可以使用以下代码检查 `nodes1`,`nodes2`,`nodes3` 中的节点是否存在重复:
```
if size(unique(nodes1, 'rows'), 1) ~= size(nodes1, 1)
disp('nodes1 中存在重复节点');
end
if size(unique(nodes2, 'rows'), 1) ~= size(nodes2, 1)
disp('nodes2 中存在重复节点');
end
if size(unique(nodes3, 'rows'), 1) ~= size(nodes3, 1)
disp('nodes3 中存在重复节点');
end
```
如果输出了任何一条消息,则表示存在重复节点。你还可以使用 `unique` 函数删除重复节点,以避免在计算交集时出现问题。
相关问题
错误使用 strsplit 不匹配的参数名称 'Node 8: (21.647140,78.620063,90.000000)' 必须为可表示字段名称的字符串标量或字符向量。 出错 strsplit (line 99) p.parse(varargin{:}); 出错 oooo (line 671) nodes1 = str2double(strsplit(data1{1}{2:end}));% 读取节点坐标数据 node_coordinates = importdata('node_coordinates.txt'); % 读取四个文件中的节点坐标 fid1 = fopen('important_nodes.txt', 'r'); data1 = textscan(fid1, '%s', 'Delimiter', '\n'); fclose(fid1); nodes1 = str2double(strsplit(data1{1}{2:end})); fid2 = fopen('resultjieshu1.txt', 'r'); data2 = textscan(fid2, '%s', 'Delimiter', '\n'); fclose(fid2); nodes2 = str2double(strsplit(data2{1}{2:end})); nodes3 = []; for i = 1:length(I) nodes3 = [nodes3 I(i)]; end nodes4 = []; for i = 1:length(data) nodes4 = [nodes4 str2double(data{1}{i})]; end % 找出同时出现在四个结果中的节点坐标 common_nodes = intersect(intersect(intersect(nodes1, nodes2), nodes3), nodes4); fprintf('同时出现在四个结果中的节点坐标:\n'); disp(node_coordinates(common_nodes,:));
这个错误提示是因为 `strsplit` 函数的第一个输入参数应该是一个字符串,而在 `data1{1}{2:end}` 中包含了多个字符串,导致无法正确解析。你可以将其改为使用一个循环遍历每个字符串进行处理,或者使用其他函数来解析字符串。以下是使用循环的示例代码:
```
% 读取节点坐标数据
node_coordinates = importdata('node_coordinates.txt');
% 读取四个文件中的节点坐标
fid1 = fopen('important_nodes.txt', 'r');
data1 = textscan(fid1, '%s', 'Delimiter', '\n');
fclose(fid1);
nodes1 = [];
for i = 2:length(data1{1})
node_str = strsplit(data1{1}{i}, ': ');
node_coord = strsplit(node_str{2}, ',');
node_x = str2double(node_coord{1}(2:end));
node_y = str2double(node_coord{2});
node_z = str2double(node_coord{3}(1:end-1));
nodes1 = [nodes1; node_x, node_y, node_z];
end
fid2 = fopen('resultjieshu1.txt', 'r');
data2 = textscan(fid2, '%s', 'Delimiter', '\n');
fclose(fid2);
nodes2 = [];
for i = 2:length(data2{1})
node_str = strsplit(data2{1}{i}, ',');
node_id = str2double(node_str{1}(4:end));
node_x = str2double(node_str{2}(2:end));
node_y = str2double(node_str{3}(2:end));
node_z = str2double(node_str{4}(2:end-1));
nodes2 = [nodes2; node_x, node_y, node_z];
end
nodes3 = [];
for i = 1:length(I)
node_id = idx(top10_idx(i));
node_coord = node_pos(top10_idx(i), :);
nodes3 = [nodes3; node_coord];
end
nodes4 = [];
for i = 1:length(data)
node_str = strsplit(data{1}{i}, ': ');
node_coord = strsplit(node_str{2}, ',');
node_x = str2double(node_coord{1}(2:end));
node_y = str2double(node_coord{2});
node_z = str2double(node_coord{3}(1:end-1));
nodes4 = [nodes4; node_x, node_y, node_z];
end
% 找出同时出现在四个结果中的节点坐标
common_nodes = intersect(intersect(intersect(nodes1, nodes2, 'rows'), nodes3, 'rows'), nodes4, 'rows');
fprintf('同时出现在四个结果中的节点坐标:\n');
disp(common_nodes);
```
这个代码将每个字符串分别解析为节点编号和节点坐标,然后将节点坐标存储在 `nodes1`、`nodes2`、`nodes3` 和 `nodes4` 中。在找到这四个变量的交集时,使用了 `'rows'` 参数来指定对每个节点的三个坐标进行比较。
subgraph(subset, edge_index, edge_attr=None, relabel_nodes=False, num_nodes=None)
`subgraph`是PyTorch Geometric中的一个函数,用于从一个大的图中提取子图。
该函数的输入参数包括:
- `subset`: 待提取子图的节点列表,类型为`LongTensor`,形状为`(num_nodes,)`。
- `edge_index`: 原始图的边列表,类型为`LongTensor`,形状为`(2, num_edges)`,其中第一行为源节点索引,第二行为目标节点索引。
- `edge_attr`: 原始图的边属性列表,类型为`Tensor`,形状为`(num_edges, ...)`
- `relabel_nodes`: 是否重新对子图中的节点进行编号,类型为`bool`。
- `num_nodes`: 子图中节点的数量,类型为`int`。
函数返回一个包含子图中节点和边的新图对象。
使用示例:
```python
import torch
from torch_geometric.utils import subgraph
# 原始图数据
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]])
edge_attr = torch.tensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]])
# 从原始图中提取节点索引为[0, 2]的子图
subset = torch.tensor([0, 2])
sub_data = subgraph(subset, edge_index, edge_attr)
# 打印子图的节点和边信息
print(sub_data)
```
输出结果:
```
Batch(batch=[2], edge_attr=[2, 2], edge_index=[2, 2])
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)