【问题标题】:"Conversion to double from cell is not possible." but input is cell array of doubles“从单元格转换为双倍是不可能的。”但输入是双精度元胞数组
【发布时间】:2016-12-09 07:44:52
【问题描述】:

我有以下代码:

Xh = zeros(InfDS.statedim,N);                          
Xh(:,1) = str2double(states_0(2:end,2));
Px = diag(str2double(states_0(2:end,3)));

其中states_0 是一个 Nx3 元胞数组。例如,它可能如下所示:

''       'x0'       'P0'  
'SOC'    '100'      '0.75'
'RI'     '0.001'    '0.75'

但它也可以包含更多参数。

我的问题是我重写了部分代码,s.t. in 不再返回包含字符串的元胞数组,而是包含双精度的元胞数组。

[       100]    [0.7500]
[1.0000e-03]    [0.7500]

我认为由于转换次数较少,这会更容易使用,但似乎不是......我已经尝试过:

Xh = zeros(InfDS.statedim,N);
Xh(:,1) = states_0(1:end,1);
Px = diag(states_0(1:end,2));

但这给了我错误:

Conversion to double from cell is not possible.

Error in testfile (line 61)
Xh(:,1) = states_0(1:end,1);

有人能解释一下为什么会这样吗?我认为这会是一种更好的做事方式,但它似乎完全违法......

【问题讨论】:

  • 请参阅This doc,了解如何索引元胞数组

标签: matlab cell-array


【解决方案1】:

似乎 cell2mat() 可以解决问题!

注意:我猜 string2double 隐式转换为数组?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多