Add value to cell in a loop

非 Y 不嫁゛ 提交于 2021-02-11 15:21:15

问题


I have simple code as below and try to insert the values into cell array.

a = cell(14,1);
for i = 1:14
    a(i:1)=sin(i)
end

However error came out as:

Conversion to cell from double is not possible.

What is the problem for this code?


回答1:


Either expand the cell, or wrap the result of the sin function in a cell.

a = cell(14,1);
b = cell(14,1);

for ii = 1:14
    a{ii} = sin(ii);
    b(ii) = {sin(ii)};
end

isequal(a,b)

ans =

  logical

   1



回答2:


Your Syntax is wrong. a(i:1) can not work inside a loop over i. Simply using a(i) will give you the desired result.



来源:https://stackoverflow.com/questions/55184390/add-value-to-cell-in-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!