inds = find(cameFrom);
时间: 2023-12-04 15:02:41 浏览: 90
在 MATLAB 中,`find` 函数可以用于查找数组中非零元素的索引。但是在这里,`cameFrom` 并不是一个数组,而是一个字典,因此 `find` 函数无法直接使用。
如果要获取 `cameFrom` 中非空元素的索引,可以使用 `keys` 函数获取所有的键(即节点),然后利用 `isKey` 函数判断该节点是否有前驱节点,如果有,则将该节点的索引存储在数组 `inds` 中。
具体实现过程可以参考以下代码:
```matlab
function inds = find_nonempty(cameFrom)
keys = cameFrom.keys;
inds = [];
for i = 1:length(keys)
if isKey(cameFrom, keys{i})
inds = [inds, i];
end
end
end
```
该函数会遍历所有节点,判断该节点是否有前驱节点,如果有,则将该节点的索引存储在数组 `inds` 中,并返回非空元素的索引。
相关问题
function p = get_path(cameFrom, current) % Returns the path. This function is only called once and therefore % does not need to be extraordinarily efficient inds = find(cameFrom); p = nan(1, length(inds)); p(1) = current; next = 1; while any(current == inds) current = cameFrom(current); next = next + 1; p(next) = current; end p(isnan(p)) = []; end逐行解释这段代码
这段代码是一个 MATLAB 函数,输入参数为一个字典 `cameFrom` 和一个节点 `current`,输出参数为一个数组 `p`,表示从起点到终点的最短路径。
下面是逐行解释:
```matlab
function p = get_path(cameFrom, current) % Returns the path. This function is only called once and therefore
```
定义一个函数 `get_path`,输入参数为一个字典 `cameFrom` 和一个节点 `current`,输出参数为一个数组 `p`,表示从起点到终点的最短路径。这个函数只会被调用一次,因此不需要特别高效的实现。
```matlab
inds = find(cameFrom);
```
使用 MATLAB 内置函数 `find` 获取字典 `cameFrom` 中所有非零元素的索引,存储在数组 `inds` 中。
```matlab
p = nan(1, length(inds));
```
使用 MATLAB 内置函数 `nan` 创建一个值全为 NaN 的数组 `p`,长度为 `inds` 数组的长度,用于存储最短路径。
```matlab
p(1) = current;
```
将终点 `current` 存储在数组 `p` 的第一个元素中,表示最短路径的最后一个节点是终点。
```matlab
next = 1;
```
定义变量 `next`,表示要存储到数组 `p` 的下一个节点的位置。
```matlab
while any(current == inds)
```
判断当前节点 `current` 是否在 `inds` 数组中,即当前节点是否有前驱节点,如果有,则继续遍历前驱节点。
```matlab
current = cameFrom(current);
```
将当前节点更新为它的前驱节点。
```matlab
next = next + 1;
```
`next` 变量加 1,表示要存储到数组 `p` 的下一个节点的位置。
```matlab
p(next) = current;
```
将当前节点 `current` 存储在数组 `p` 的 `next` 位置中。
```matlab
end
```
当当前节点 `current` 不在 `inds` 数组中时,说明已经遍历到了起点,退出循环。
```matlab
p(isnan(p)) = [];
```
删除数组 `p` 中的 NaN 值,即删除未被存储节点的部分,得到最短路径数组 `p`。
```matlab
end
```
函数结束。
将下列numpy代码转化为纯pytorch代码: inds = np.where(scores > args.confidence_threshold)[:] boxes = boxes[inds] landms = landms[inds] scores = scores[inds]
请使用 PyTorch 的 mask 和 index_select 函数来替换 numpy 中的 where 和索引选择。
```
import torch
# 将 scores 转化为 PyTorch tensor
scores = torch.tensor(scores)
# 创建一个 mask,用来筛选出 scores 大于 args.confidence_threshold 的元素
mask = (scores > args.confidence_threshold)
# 使用 mask 和 index_select 函数来筛选出 boxes、landms 和 scores
boxes = boxes[mask]
landms = landms[mask]
scores = scores[mask]
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)