please help

**Info**

This question is closed. Reopen it to edit or answer.

# Write a function called sparse2matrix that takes a single input of a cell vector as defined above and returns the output argument called matrix, the matrix in its traditional form

1 view (last 30 days)

Show older comments

cellvec = {[2 3], 0, [1 2 3], [2 2 -3]};

matrix = sparse2matrix(cellvec)

matrix =

0 3 0

0 -3 0

function [matrix]=sparse2matrix(incell)

msize = incell{1};

mdef = incell{2};

matrix = repmat(mdef,msize);

for n = 3:numel(incell)

RCV = incell{n};

end

matrix = sparse2matrix({[2 3], 0, [1 2 3], [2 2 -3]})

matrix =

0 0 0

0 0 0

Assessment result: incorrectA few simple cases

Variable solution has an incorrect value.

sparse2matrix( { [ 3 4 ], 0, [ 2 2 -3 ], [ 1 3 3 ] } ) failed...

Assessment result: incorrectRandom cases

Variable solution has an incorrect value.

sparse2matrix( { [ 10 10 ], 3, [ 9 9 0 ], [ 9 8 8 ], [ 8 6 -7 ], [ 7 7 4 ], [ 1 1 0 ], [ 4 8 7 ], [ 1 4 1 ], [ 4 8 -1 ], [ 8 7 6 ] } ) failed..

##### 6 Comments

Rik
on 14 Jun 2020

### Answers (10)

Arafat Roney
on 11 May 2020

function matrix=sparse2matrix(p)

m=p{1}(1,1);

n=p{1}(1,2);

o=p{2}(1,1);

s=o.*ones(m,n);

for i=3:length(p)

r=p{i}(1,1);

c=p{i}(1,2);

v=p{i}(1,3);

s(r,c)=v;

end

matrix=s;

end

##### 0 Comments

Emine Ertugrul
on 17 Apr 2020

function matrix = sparse2matrix(cell)

matrix = cell{2}*ones(cell{1}(1),cell{1}(2))

for ii = 3:length(cell)

matrix(cell{ii}(1),cell{ii}(2)) = cell{ii}(3);

end

##### 1 Comment

Rik
on 17 Apr 2020

There are several issues with this answer. Firstly it is not formatted correctly, making it less readable. Secondly it seems intended to be a fully working solution to a homework question, encouraging cheating.

But more importantly, it is using cell as a variable name while using the cell data type. This will very likely lead to confusion. Also, since the question definition is not entirely clear, it is possibly not the correct answer to the question.

Saibalaji Kokate
on 23 Apr 2020

function ans=sparse2matrix(m)

x=m{1,1};

class(x)

val=m{1,2};

mat=repmat(val,x);

len=length(m);

for y=3:len

a=m{y};

row=a(1,1);

col=a(1,2);

mat(row,col)=a(1,3);

end

ans=mat;

end

##### 0 Comments

Soham Khadilkar
on 26 Apr 2020

Edited: Soham Khadilkar
on 26 Apr 2020

function matrix = sparse2matrix(cellvec)

r = cellvec{1,1}(1,1);

c = cellvec{1,1}(1,2);

[e,l] = size(cellvec)

matrix = ones(r,c)*cellvec{1,2};

for i= 3:l

r1 = cellvec{1,i}(1,1);

c1 = cellvec{1,i}(1,2);

matrix(r1,c1) = cellvec{1,i}(1,3);

i=i+1;

end

%This works for any length of the cellvec

%the code is probably a little long so suggest some stuff to make it short.

##### 2 Comments

Rik
on 23 Jun 2020

Ved Prakash
on 15 May 2020

function matrix=sparse2matrix(P)

r=P{1}(1);

c=P{1}(2);

D=P{2}*ones(r,c);

for i=3:length(P)

D(P{i}(1),P{i}(2))=P{i}(3);

end

matrix=D;

##### 0 Comments

Syed Zuhair Ali Razvi
on 22 May 2020

function mat=sparse2matrix(cellvec)

m1=zeros(cellvec{1});

m2=cellvec{2}+m1;

if length(cellvec)<=2

mat=m2;

else

for i=cellvec(3:end)

for n=1:i{end}

a1=i{1,n}(1,1);

a2=i{1,n}(1,2);

m2(a1,a2)=i{n}(1,3);

mat=m2;

n=n+1;

break

end

end

end

end

##### 1 Comment

Rik
on 22 May 2020

Tahsin Oishee
on 3 Jun 2020

function matrix=sparse2matrix(x)

matrix=zeros(x{1})

matrix(1:end)=x{2}

a=length(x);

i=3;

for i=3:a

matrix(x{1,i}(1),x{1,i}(2))=x{1,i}(1,3)

i=i+1

end

end

##### 1 Comment

Raymond He
on 8 Jun 2020

function matrix = sparse2matrix(a)

matrix = a{2}(1,1) * ones(a{1}(1,1),a{1}(1,2));

for i = 3:length(a)

matrix(a{i}(1,1),a{i}(1,2)) = a{i}(1,3);

end

end

##### 0 Comments

UJJWAL Padha
on 10 Jun 2020

Edited: UJJWAL Padha
on 10 Jun 2020

function matrix = sparse2matrix(a)

sparse_matrix = 0; % assigning variable sparse_matrix = 0

for i = 1:a{1,1}(1,1) %running for loop from 1st to the first element of vector(i.e rows of matrix) assigned to first cell of a

for j = 1:a{1,1}(1,2) %running for loop from 2nd to the first element of vector(i.e columns of matrix) assigned to first cell of a

sparse_matrix(i,j) = a{1,2} ; %here all the elements of sparse_matrix will become default i.e 2nd cell of a

end

end

for l= 3:length(a) % running for loop from 3 to length of a

sparse_matrix(a{1,l}(1,1) , a{1,l}(1,2)) = a{1,l}(1,3); %as the loop runs from 3 to length of a the elements, the non-default values will be assigned to respective elements of sparsematrix

end

matrix = sparse_matrix;

end

##### 4 Comments

UJJWAL Padha
on 11 Jun 2020

i understand now.....

Thankyou for sharing this piece of knowledge....

Appreciate it :)

Md Nazmus Sakib
on 19 Jun 2020

function y = sparse2matrix(p)

row = p{1,1}(1,1);

col = p{1,1}(1,2);

default_val = p{1,2};

matrix = [];

%assigning default value to all positions

for i = 1 : row

for j = 1 : col

matrix(i,j) = default_val;

end

end

row_mat = [];

col_mat = [];

val_mat = [];

%fetching all rows info and creating a vector

for m = 1 : length(p)

if ((m == 1) || (m == 2))

continue

else

row_mat(m) = p{1,m}(1,1);

end

end

%fetching all columns info and creating a vector

for n = 1 : length(p)

if ((n == 1) || (n == 2))

continue

else

col_mat(n) = p{1,n}(1,2);

end

end

%fetching all values and creating a vector

for no = 1 : length(p)

if ((no == 1) || (no == 2))

continue

else

val_mat(no) = p{1,no}(1,3);

end

end

%rewriting the values to corresponding positions

for x = 3 : length(row_mat)

matrix(row_mat(x),col_mat(x)) = val_mat(x);

end

y = matrix;

end

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!