371 views (last 30 days)

Write the function for

A sparse matrix is a large matrix with almost all elements of the same value (typically zero). The normal representation of a sparse matrix takes up lots of memory when the useful information can be captured with much less. A possible way to represent a sparse matrix is with a cell vector whose first element is a 2-element vector representing the size of the sparse matrix. The second element is a scalar specifying the default value of the sparse matrix. Each successive element of the cell vector is a 3-element vector representing one element of the sparse matrix that has a value other than the default. The three elements are the row index, the column index and the actual value. 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. Consider the following run:

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

matrix = sparse2matrix(cellvec)

matrix =

0 3 0

0 -3 0

stanleo
on 7 Jul 2019

%simple version

function matrix = sparse2matrix(cellvec)

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

for m=3:size(cellvec,2)

matrix(cellvec{m}(1),cellvec{m}(2))=cellvec{m}(3);

end

Rik
on 29 Aug 2020

Walter Roberson
on 29 Aug 2020

Bhoomika:

We do not have any idea what your level of experience in programming is. We would have to start from the basics of mathematics and computer science to explain the entire code in a way that we could relatively sure you would understand. That would take at least two textbooks of explanation. None of us has time to write all that.

We suggest you ask more specific questions that can be more easily answered.

AYUSH GURTU
on 28 May 2019

function [matrix]=sparse2matrix(incell)

S=size(incell);

q=S(2)-2;

msize=incell{1};

mdef=incell{2};

matrix=repmat(mdef,msize);

while q>0

matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);

q = q-1;

end

Pavel Radko
on 13 Aug 2020

Edited: Pavel Radko
on 13 Aug 2020

Passed all tests solution. May be not the best one (because I have no idea how to biuld default matrix in easier way), but works 100%.

% Build a matrix called "matrix" using instrictions of input "cellvec"

function matrix = sparse2matrix(cellvec)

% first we build a default matrix with size ii*jj

% we use 1st element of "cellvec" to get the size of matrix

for ii = 1:cellvec{1}(1,1)

for jj = 1:cellvec{1}(1,2)

% all elements of matrix equals to the 2nd element of "cellvec"

matrix(ii,jj) = cellvec{2};

end

end

% now we need to change elements of our default matrix

% instructions for place and value of this elements comes in "cellvec"

% from 3rd element till the end of "cellvec"

for zz = 3:length(cellvec)

% we call "matrix" elements and assign values to them from every 3rd element of subarrays of "cellvec"

matrix(cellvec{zz}(1,1),cellvec{zz}(1,2)) = cellvec{zz}(1,3);

end

end

THIERNO AMADOU MOUCTAR BALDE
on 29 Dec 2020 at 13:03

working for the example given in the problem but others no thanks for sharing

Abhishek singh
on 24 Apr 2019

function [matrix]= sparse2matrix(incell);

X=size(incell);

q=X(2)-2;

msize=incell{1};

mdef=incell{2};

matrix=repmat(mdef,msize);

while q > 0

matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);

break

end

output

matrix =

0 0 0

0 -3 0

required output

matrix =

0 3 0

0 -3 0

Abhishek singh
on 24 Apr 2019

# added q+1

function [matrix]= sparse2matrix(incell);

X=size(incell);

q=X(2)-2;

msize=incell{1};

mdef=incell{2};

matrix=repmat(mdef,msize);

while q > 0

matrix(incell{q+1}(1), incell{q+1}(2)) = incell{q+1}(3);

matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);

break

end

matrix =

0 3 0

0 -3 0

but failed for

ariable solution has an incorrect value.

sparse2matrix( { [ 9 12 ], 3, [ 6 2 6 ], [ 7 1 -6 ], [ 1 10 -7 ], [ 2 2 -3 ], [ 1 4 -8 ], [ 1 11 -8 ], [ 9 11 -8 ], [ 7 8 5 ], [ 9 8 4 ], [ 9 11 7 ], [ 5 9 -4 ], [ 8 12 8 ], [ 3 6 5 ] } ) failed...

Walter Roberson
on 24 Apr 2019

Why are you using break after one iteration of the loop ? If you are only going to do a set of instructions once, do not bother to put it in a loop.

I suggest that you read about for loops.

Jaimin Motavar
on 30 Jun 2019

Edited: Jaimin Motavar
on 30 Jun 2019

can you tell me what is wrong in this answer?

function matrix = sparse2matrix(a)

e=length(a);

b=rand(a{1,1});

[m,n]=size(b);

c=a{1,3};

d=a{1,4};

for i=1:m

for j=1:n

b(i,j)=a{1,2};

end

end

for g=3:e

for f=(g-2):(e-2)

p(1,f)=a{1,g}(1,1);

end

end

for g=3:e

for f=(g-2):(e-2)

q(1,f)=a{1,g}(1,2);

end

end

for g=3:e

for f=(g-2):(e-2)

r(1,f)=a{1,g}(1,3);

end

end

for o=1:(e-2)

b(p(o),q(o))=r(o);

end

matrix=b;

end

Litesh Ghute
on 20 Mar 2020

What's wrong with my code ?

function matrix= sparse2matrix(v)

mat=zeros([v{1}(1),v{1}(2)]);

r=size(mat);

m=3;

while m <= 4

i=v{m}(1);

j=v{m}(2);

mat(v{m}(i,j))=v{m}(3);

m=m+1;

end

matrix=mat;

end

ABINAND PANDIYAN
on 23 Apr 2020

Edited: ABINAND PANDIYAN
on 23 Apr 2020

%All the test cases are right. try this

function matrix= sparse2matrix(cellvec)

a= cellvec{1};

row=a(1);

column=a(2);

main_value= cellvec{2};

sparse_matrix= main_value * ones(row, column);

len= length(cellvec);

for i= 3:length(cellvec)

change = cellvec{i};

r=change(1);

c=change(2);

m=change(3);

sparse_matrix(r,c)=m;

end

matrix=sparse_matrix;

end

THIERNO AMADOU MOUCTAR BALDE
on 29 Dec 2020 at 13:09

thank you so much!

it is working just one suggestion for using the variable len

len = length(cellvec);

for i = i= 3:len

...

......

end

SAMARTH MAHESHKUMAR GEMLAWALA
on 15 May 2020

% Compteled all the test cases successfully.

function matrix = sparse2matrix(a)

cellvec = a

p= size(cellvec)

z = cellvec{1,1}

x = cellvec{1,2}

matrix = zeros(z(1,1),z(1,2));

for i=1:z(1,1)

for j= 1:z(1,2)

matrix(i,j) = x;

end

end

for j= 3: p(1,2)

y = cellvec{1,j}

matrix(y(1,1),y(1,2)) = y(1,3);

end

Priyansh Kushwaha
on 16 May 2020

Edited: Priyansh Kushwaha
on 17 May 2020

function matrix=sparse2matrix(a)

b=a{1,1}

b1=b(1,1);

b2=b(1,2);

e=ones(b1,b2);

b=a{1,2}

e=b.*(e);

for i=3:length(a)

c=a{1,i};

d1=c(1,1);

d2=c(1,2);

d3=c(1,3);

e(d1,d2)=d3;

matrix=e;

end

matrix=e;

end

Walter Roberson
on 17 May 2020

matrix=e;

You overwrite matrix after the loop, so there is no point in doing that assignment inside the loop.

Priyansh Kushwaha
on 17 May 2020

utkarsh singh
on 21 May 2020

Edited: utkarsh singh
on 21 May 2020

function matrix=sparse2matrix(a)

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

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

default=a{1,2}; % or simply default=a{1,2}

matrix=ones(row,col)*default; % matrix=ones(a{1})*deafult.....no need of finding row and col

for m=3:length(a)

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

end

Taif Ahmed BIpul
on 24 May 2020

function matrix=sparse2matrix(cellvec)

m=ones(cellvec{1}(1),cellvec{1}(2));

m=m.*cellvec{2};

for i=3:length(cellvec)

m(cellvec{i}(1),cellvec{i}(2))=cellvec{i}(3);

end

matrix=m;

ahmed mamdouh
on 7 Jun 2020

function matrix = sparse2matrix(ce)

matri=ones(ce{1,1}(1,1),ce{1,1}(1,2))*ce{1,2};

siz=size(ce);

i=siz(1,2);

for ii=3:i

matri( ce{1,ii}(1,1),ce{1,ii}(1,2))=ce{1,ii}(1,3);

end

matrix=matri;

Shikha Thapa
on 13 Jun 2020

function matrix=sparse2matrix(cellvec)

matrix=cellvec{1,2}*ones([cellvec{1}(1),cellvec{1}(2)]);

for a=3:length(cellvec)

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

end

Kumar Shubham
on 12 Jul 2020

Edited: Kumar Shubham
on 12 Jul 2020

function matrix = sparse2matrix(cellvec)

%creates matrix of req. size.

matrix=zeros(cellvec{1});

%allots assigned scalar value to all elements.

matrix(:)=deal(cellvec{2});

%used loop to maipulate matrix for result.

%use breakpoints to see approach to result step by step .

for ii = 3:length(cellvec)

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

end

Walter Roberson
on 12 Jul 2020

Why are you using deal? Are you expecting that cellvec{2} will expand to multiple comma-separated elements? That is not going to happen with a scalar index like {2}

If you are wanting to copy the one value expectd in cellvec{2} to all elements on the left, then you do not need deal() .

Ishani Uthpala
on 1 Aug 2020

function matrix=sparse2matrix(v)

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

y=length(v);

for x=3:y

matrix(v{1,x}(1,1),v{1,x}(1,2))=v{1,x}(1,3);

x=x+1;

end

matrix;

end

Walter Roberson
on 1 Aug 2020

What is the purpose of your line

x=x+1;

??

What is the purpose of your line

matrix;

??

sushmanth pulavarthi
on 3 Aug 2020

function matrix=sparse2matrix(v)

rows=v{1,1}(1);columns=v{1,1}(2); %extracting total no.of rows and columns for sprase matrix

magnitude=v{2}; %extracting the default value

m=magnitude*ones(rows,columns);

for i=3:length(v) %creating the loop foor changing the values other than default

r=v{i}(1);

c=v{i}(2);

m(r,c)=v{i}(3);

end

matrix=m;

end

%this works for any no.of elements

A.H.M.Shahidul Islam
on 6 Aug 2020

% 100% accurate

function matrix=sparse2matrix(m)

m=cell(m);

r=m{1}(1);c=m{1}(2);dv=m{2};

ss=size(m);

matrix=sparse(r,c)+dv;

q=ss(1,2);

for ii=3:q

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

end

Rik
on 6 Aug 2020

Thanks, now I can cheat on my homework without having to bother understanding the problem or the solution.

On a slightly more serious note: you forgot the closing end. Although you don't need it, it has become a lot more common, especially since it is possible to put functions in script files.

Ali Raza
on 9 Sep 2020

function matrix = sparse2matrix(x)

M = x{1};

m = ones(M(1),M(2)) * x{2};

[~,len] = size(x);

if len == 3

i = 3;

m(x{i}(1),x{i}(2)) = x{i}(3);

else

for i = 3:len

m(x{i}(1),x{i}(2)) = x{i}(3);

end

end

matrix = m;

end

Abdul Quadir Khan
on 6 Nov 2020

function matrix = sparse2matrix (cellvec)

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

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

defult = ones(m,n) .* cellvec{1,2};

for i= 3:length(cellvec)

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

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

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

end

matrix = defult;

end

zehra ülgen
on 12 Nov 2020

Here is another solution..

function m = sparse2matrix(a)

[t c] = size(a);

m = zeros(a{1,1});

[x y] = size(m);

for ii = 1:x;

for jj = 1:y;

m(ii,jj) = a{1,2};

end

end

for i = 3:c;

v = a(1,i);

m(v{1,1}(1,1),v{1,1}(1,2)) = v{1,1}(1,3);

end

Alberto Gil
on 29 Dec 2020 at 22:04

Edited: Alberto Gil
on 29 Dec 2020 at 22:05

Hello people,

What do you think about this code?

function matrix=sparse2matrix(cll)

if iscell(cll)==1

% Declare values, cs=size of the array; cdn=the nominal value; N=greatest value;

cs=cll{1,1}; cdn=cll{1,2}; N=size(cll,2);

% Create the matrix with the nominal value and the size.

cm=ones(cs)*cdn;

for n=3:N;

cxdn=cll{1,n};

% Select the values of the input values.

cm(cxdn(1,1), cxdn(1,2))=cxdn(1,3);

end

matrix= cm;

else

error('The input must be a cell class');

end

end

Walter Roberson
on 30 Dec 2020 at 21:15

The question does not seem to require that you verify that the input is a cell.

xin yi leow
on 19 Jan 2021 at 17:46

function matrix=sparse2matrix(cellx)

matrix=zeros(cellx{1});

matrix(:,:)=cellx{2};

for ii=3:length(cellx)

num=cellx{ii};

matrix(num(1),num(2))=num(3);

end

end

Rik
on 19 Jan 2021 at 18:10

What does this answer add? What does it teach? Why should it not be deleted?

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

Start Hunting!
## 15 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696278

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696278

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696281

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696281

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696285

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696285

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696293

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_696293

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_716894

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_716894

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_719166

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_719166

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_728073

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_728073

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_728105

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_728105

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_842901

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_842901

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848982

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848982

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848984

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848984

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848990

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_848990

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_849091

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_849091

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_849095

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_849095

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_993104

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/457568-i-get-an-error-what-s-wrong-on-sparse-matrix-logic-and-answer#comment_993104

Sign in to comment.