将向量解包为k个变量

时间:2018-02-13 05:17:21

标签: matlab vector vectorization

我正在学习matlab并尝试使用矢量化来实现small angle approximation。这是我试图实施的公式:

enter image description here

我实现了3个角度:pi / 100pi / 10pi / 6。到目前为止,我已经提出了这个问题:

x = [pi / 100, pi / 10, pi / 6];
y = sin(x);
e = (y - x) ./ y;

这很好用。现在,我想将结果解压缩为3个变量。我知道我可以这样做:

RelativeError100 = e(1);
RelativeError10 = e(2);
RelativeError6 = e(end);

但是从蟒蛇背景来看,这对我来说似乎有点笨拙。所以我尝试了这个:

[RelativeError100, RelativeError10, RelativeError6] = e

出错Too many output arguments.。所以,我做了一些研究,并发现this暗示

[RelativeError100, RelativeError10, RelativeError6] = e{:}

引发Cell contents reference from a non-cell array object.

我错过了什么,我怎样才能让它发挥作用?或者用matlab是不可能的?

1 个答案:

答案 0 :(得分:3)

怎么样:

x = [pi / 100, pi / 10, pi / 6];
y = sin(x);
e = num2cell((y - x) ./ y);
[RelativeError100, RelativeError10, RelativeError6] = e{:}

因为{:}仅在ecell而非matrix时才有效。