conversion from double to cell is not possible

5 次查看(过去 30 天)
Hi,
I have just started to learn matlab. I get the folowing error when the below mentioned function is executed.
Error using cell
Conversion to double from cell is not possible.
Error in sparse2matrix (line 2)
num_array(y)=cell(a(1,1),a(1,2));
when this statement is executed separately no such errors occur.
plzz help me .
Thank you in advance.
function x=sparse2matrix(a,b,c,d)
y=cell(a(1,1),a(1,2));
y(:)={b};
y{c(1,1),c(1,2)}={c(1,3)};
y{d(1,1),d(1,2)}={d(1,3)};
x=cell2mat(y);
end
  2 个评论
Venkatesh K
Venkatesh K 2020-1-5
a,b,c and d are row vectors
eg:
sparse2matrix({[2 3], 0, [1 2 3], [2 2 -3]});

请先登录,再进行评论。

回答(1 个)

Bhaskar R
Bhaskar R 2020-1-5
I don't know what you are trying to do with given code, if I assume by name of the mentioned function name "sparse to matrix conversion", to do this sparse to matrix conversion MATLAB has a command
full
Issues in your code
you have passed variables to function as
sparse2matrix({[2 3], 0, [1 2 3], [2 2 -3]}); % actually it is only one input
This means you are paasing only single variable as cell type to the function, i.e. single varible consist 4 values in it
so if you want to pass variables as defined in function call the function as
sparse2matrix([2 3], 0, [1 2 3], [2 2 -3]); % remove {}, which makes cell array
And one more issue with code is values assigned as cell type so modify as
function x=sparse2matrix(a,b,c,d)
y=cell(a(1,1),a(1,2));
y(:)={b};
y(c(1,1),c(1,2))={c(1,3)}; % {} replaced with ()
y(d(1,1),d(1,2))={d(1,3)}; % {} replaced with ()
x=cell2mat(y);
end
% Note : it's assumption only to get error output

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by