错误使用 - 矩阵维度必须一致。 出错 quanguocheng (line 29) projection = section_start + dot(points(j, :) - section_start, section_end - section_start) / norm(section_end - section_start)^2 * (section_end - section_start);
时间: 2024-03-04 17:48:44 浏览: 20
这个错误是因为 `section_start`、`section_end` 和 `points(j, :)` 的维度不同导致的。根据代码,`section_start` 和 `section_end` 是一个 1x1x3 的三维数组,而 `points` 是一个 1000x3 的数组。
为了解决这个问题,可以将 `section_start` 和 `section_end` 的维度调整为 1x3 的数组,使其与 `points(j, :)` 的维度一致。可以使用 `squeeze` 函数来实现:
```matlab
section_start = squeeze(sections(i, 1, :))';
section_end = squeeze(sections(i, 2, :))';
```
其中,`squeeze` 函数可以去掉数组中维度为 1 的维度。然后,`'` 操作符可以将结果转置,使其成为一个 1x3 的数组。
相关问题
glm::vec2 Projection_PointC = Point_A + glm::dot(Normal_AB, turn_Circle_Vertex.Center_Position - Point_A) * Normal_AB; 这行代码什么意思
这行代码是用来计算将一个点投影到一个线段上的最近点。具体来说,它使用了点积运算和向量加法来实现。
- `Point_A` 是线段的起始点。
- `Normal_AB` 是线段的方向向量,即由起点指向终点的向量。
- `turn_Circle_Vertex.Center_Position` 是要进行投影的点。
- `glm::dot(Normal_AB, turn_Circle_Vertex.Center_Position - Point_A)` 计算了点积,表示了 `turn_Circle_Vertex.Center_Position` 相对于线段的投影长度(在 `Normal_AB` 方向上)。
- `Projection_PointC = Point_A + glm::dot(Normal_AB, turn_Circle_Vertex.Center_Position - Point_A) * Normal_AB` 将投影长度乘以方向向量,并将结果加到起始点上,得到最终的投影点 `Projection_PointC`。
换句话说,这行代码的作用是根据给定的线段和要投影的点,计算出将该点投影到该线段上的最近点。
tgt_in = torch.rand((Batch_size, 1, 3))这是输入张量, def encoder_in(self, src): src_start = self.input_projection(src).permute(1, 0, 2) in_sequence_len, batch_size = src_start.size(0), src_start.size(1) pos_encoder = (torch.arange(0, in_sequence_len, device=src.device).unsqueeze(0).repeat(batch_size, 1)) pos_encoder = self.input_pos_embedding(pos_encoder).permute(1, 0, 2) src = src_start + pos_encoder src = self.encoder(src) + src_start return src这部分改怎么修改不会出错误?
在你提供的代码中,有一些问题可能导致错误。首先,`src_start` 是通过对 `src` 进行投影操作后进行维度变换,但是这里的维度变换是不正确的。正确的是将维度 `(in_sequence_len, batch_size, feature_dim)` 变换为 `(batch_size, in_sequence_len, feature_dim)`,可以使用 `permute(1, 0, 2)` 实现这个变换。
其次,`pos_encoder` 是一个位置编码张量,用于为输入序列添加位置信息。在这里,`pos_encoder` 的维度变换也是不正确的。正确的是将维度 `(batch_size, in_sequence_len, feature_dim)` 变换为 `(in_sequence_len, batch_size, feature_dim)`,可以使用 `permute(1, 0, 2)` 实现这个变换。
下面是修改后的代码片段:
```python
def encoder_in(self, src):
src_start = self.input_projection(src).permute(1, 0, 2)
in_sequence_len, batch_size = src_start.size(0), src_start.size(1)
pos_encoder = torch.arange(0, in_sequence_len, device=src.device).unsqueeze(0).repeat(batch_size, 1)
pos_encoder = self.input_pos_embedding(pos_encoder).permute(1, 0, 2)
src = src_start + pos_encoder
src = self.encoder(src) + src_start
return src
```
通过这些修改,你应该能够避免 "number of dims don't match in permute" 错误。请确保在其他部分的代码中,维度匹配和操作正确,以避免其他潜在的错误。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)