[英]MATLAB: using the find function to get indices of a certain value in an array
[英]Matlab map find value for a certain key using char array as key
C = {'hello', 'goodbye', 'hola', 'hello hellen', 'helmet', 'hellorheaven', 'hillsboro', 'say hello', 'myfellow'}
defaultval = 100
key = {'hello', 'goodbye', 'hola', 'hello hellen', 'helmet', 'hellorheaven', 'hillsboro', 'say hello', 'myfellow'}
value = [defaultval, defaultval, defaultval,defaultval,defaultval,defaultval,defaultval,defaultval,defaultval]
mapObj = containers.Map(key,value )
for n = 1:length(C)
d1 = strdist('goodfellow', C(n) )
disp(C(n) )
disp(mapObj('hello' ) )
mapObj(C(n) ) = d1
end
在這種情況下,我嘗試使用庫函數計算字符串距離,並將該距離保存到我創建的地圖中。 但是,即使C是一個char數組,我的地圖的鍵類型也是如此。 我不能使用mapObj(C(n))訪問和更改我的值。 我該如何解決?
mapObj =
Map with properties:
Count: 9
KeyType: char
ValueType: double
我試圖在這里遵循示例https://www.mathworks.com/help/matlab/matlab_prog/modifying-keys-and-values-in-the-map.html where ticketMap('A479GY')
回答=
莎拉·拉瑟姆(Sarah Latham)
但是,使用我的代碼,我收到的錯誤
使用容器時出錯。Map/ subsasgn指定的鍵類型與此容器預期的類型不匹配。
運動01(第76行)中的錯誤mapObj(C(n))= d1
正如@excaza在他的評論中指出的那樣,問題全在於索引。 使用圓括號可以檢索單元格。 但是您的字典鍵實際上是作為字符串鍵入的(確切地說,是一個char數組)。 為了從單元格數組中提取一個char數組,您必須使用花括號。 有關訪問單元中數據的更多信息,請閱讀此文檔 。
keys = {'hello', 'goodbye', 'hola', 'hello hellen', 'helmet', 'hellorheaven', 'hillsboro', 'say hello', 'myfellow'};
keys_len = numel(keys);
vals = repmat(100,1,keys_len);
map = containers.Map(keys,vals);
for n = 1:keys_len
key = keys{n};
map(key) = strdist('goodfellow',key);
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.