指定的键类型与此容器matlab预期的类型不匹配 [英] Specified key type does not match the type expected for this container matlab
问题描述
我有一个 cell_array
,其中 29136x1单元格
值显示在工作区托盘中。我还在工作区托盘中有一个地图 new_labels
与 4x1 Map
。在
new_labels =
$ b打印 new_label
$ b属性地图:
计数:4
KeyType:char
ValueType:double
cell_array中的每个条目都是地图中的关键字,但问题是地图中的 keyType
的类型不匹配是char和 cell_array
的条目是单元格
。
因为这样我就无法访问地图,因此就像下面这样:
arrayfun(@(x)new_labels(x),cell_array ,'un',0);
给出错误指定的键类型与此容器的预期类型不匹配。
我尝试使用 char_cell_array = char(cell_array)
转换为char类型,但是将数组转换成大小 29136x4
,这意味着每个条目只是一个 char
,而不是一个字符串。 p>
任何帮助赞赏。
如果要使用迭代方式,您必须使用 cellfun
。 arrayfun
对数字数组进行操作。因为 cell_array
是一个单元格数组,您需要使用 cellfun
而不是 arrayfun
as cellfun
将迭代单元格数组。
然而,你真正的是在字典中指定多个键以获取相关联的值。不要使用 arrayfun / cellfun
。有一个专用的MATLAB功能设计用于占用多个键。对 containers.Map c> interface
值
$ b
out = values(new_labels,cell_array);
只需使用值(new_labels)
这将检索字典中的所有值。如果要根据输入键检索具体的值,请提供第二个输入参数,该参数是一个单元格数组,其中包含要在 containers.Map中访问的所有密钥。
对象。因为你已经有这个单元格数组,所以你只需将它作为值的第二个输入
。
运行示例
>> A = containers.Map({1,2,3,4},{'a','b','c','d'})
A =
与属性地图:
计数:4
KeyType:double
ValueType:char
>> cell_array = {1,2,2,3,3,4,1,1,1,2,2};
>> out = values(A,cell_array)
out =
'a''b'''''''''a' 'a''b''b'
I have a cell_array
for which 29136x1 cell
value shows in the workspace pallet. I also have a map new_labels
with 4x1 Map
in workspace pallet. Printing new_label
on prompt gives
new_labels =
Map with properties:
Count: 4
KeyType: char
ValueType: double
Each entry in the cell_array is the key in the map, but the problem is there a type mismatch as keyType
in map is char and entries of cell_array
are of type cell
.
Because of this I cannot access the map and hence something like the following:
arrayfun(@(x) new_labels(x), cell_array, 'un',0);
gives error Specified key type does not match the type expected for this container.
I tried converting to char type using char_cell_array = char(cell_array)
but that converts the array to be of size 29136x4
which means every entry is just one char
and not really a string.
Any help appreciated.
If you want to use the iterative way, you have to use cellfun
. arrayfun
operates on numeric arrays. Because cell_array
is a cell array, you need to use cellfun
instead of arrayfun
as cellfun
will iterate over cell arrays instead.
However, what you're really after is specifying more than one key into the dictionary to get the associated values. Don't use arrayfun/cellfun
for that. There is a dedicated MATLAB function designed to take in multiple keys. Use the values
method for that which is built-in to the containers.Map
interface:
out = values(new_labels, cell_array);
By just using values(new_labels)
, this retrieves all of the values in the dictionary. If you want to retrieve specific values based on input keys, supply a second input parameter that is a cell array which contains all of the keys you want to access in the containers.Map
object. Because you already have this cell array, you simply use this as the second input into values
.
Running Example
>> A = containers.Map({1,2,3,4}, {'a','b','c','d'})
A =
Map with properties:
Count: 4
KeyType: double
ValueType: char
>> cell_array = {1,2,2,3,3,4,1,1,1,2,2};
>> out = values(A, cell_array)
out =
'a' 'b' 'b' 'c' 'c' 'd' 'a' 'a' 'a' 'b' 'b'
这篇关于指定的键类型与此容器matlab预期的类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!