new_idx = unique(new_idx); 错误使用 cell/unique (第 85 行) 元胞数组输入必须为字符向量元胞数组。
时间: 2024-01-21 13:16:32 浏览: 29
这个错误是因为你在使用 `unique` 函数时,输入的数组是一个元胞数组,而 `unique` 函数只能对字符向量元素进行去重操作,不能对元胞数组进行操作。
解决方法是将元胞数组转换为字符向量元素。可以使用 `cellfun` 函数将每个元胞转换为字符向量,然后再将结果传递给 `unique` 函数。示例代码如下:
```
new_idx = unique(cellfun(@(x) char(x), new_idx, 'UniformOutput', false));
```
这里 `cellfun(@(x) char(x), new_idx, 'UniformOutput', false)` 表示对 `new_idx` 中的每个元胞 `x`,都执行 `char` 函数转换为字符向量,最终返回一个字符向量元胞数组。`'UniformOutput', false` 表示输出的结果是一个元胞数组。最后将返回的字符向量元胞数组传递给 `unique` 函数进行去重操作。
相关问题
new_idx = unique(cellfun(@(x) string(x), new_idx, 'UniformOutput', false)); new_x = x(new_idx,:); 错误使用 cell/unique (第 85 行) 元胞数组输入必须为 字符向量元胞数组。 ,如何改正
这个错误的原因是 `new_idx` 中的元素不是字符串类型,所以不能直接转换为字符串。你可以先将元素转换为字符串向量,然后再调用 `unique` 函数,例如:
```
new_idx = unique(cellfun(@(x) string(x), new_idx, 'UniformOutput', false));
new_x = x(new_idx,:);
```
如果你在转换为字符串向量时遇到了错误,可能是因为 `new_idx` 中有空元素或非字符串元素。你可以使用 `isempty` 和 `isstring` 函数来过滤掉这些元素,例如:
```
new_idx = unique(cellfun(@(x) isstring(x) && ~isempty(x), new_idx, 'UniformOutput', false));
new_x = x(new_idx,:);
```
new_idx = unique(cellfun(@(x) char(x), new_idx, 'UniformOutput', false)); 错误使用 cell/unique>celluniqueR2012a (第 249 行) 元胞数组输入 A 的元素必须为字符向量(字符类的行向量)。 出错 cell/unique (第 89 行) [varargout{1:nlhs}] = celluniqueR2012a(varargin{:});
这个错误提示意味着你传递给 `unique` 函数的元胞数组 `new_idx` 中包含非字符向量的元素。因此,你需要将元胞数组中的所有元素都转换为字符向量。
你可以尝试修改代码,将 `cellfun` 函数中的回调函数从 `char` 改为 `string`,以便将所有元胞数组中的元素都转换为字符串:
```
new_idx = unique(cellfun(@(x) string(x), new_idx, 'UniformOutput', false));
```
如果你使用的是旧版本的 MATLAB,可能不支持 `string` 函数,你可以改用 `cellstr` 函数将每个元素转换为字符向量:
```
new_idx = unique(cellfun(@(x) cellstr(x), new_idx, 'UniformOutput', false));
```
这样做应该可以解决你的问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)