conversion from double to cell is not possible

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 Comments

Can you provide input values, a, b, c,d?
a,b,c and d are row vectors
eg:
sparse2matrix({[2 3], 0, [1 2 3], [2 2 -3]});

Sign in to comment.

Answers (1)

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

Categories

Asked:

on 5 Jan 2020

Answered:

on 5 Jan 2020

Community Treasure Hunt

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

Start Hunting!