Info

# 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)
Abhishek singh on 21 Apr 2019
Closed: Rik on 23 Jun 2020
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..
Walter Roberson on 21 Apr 2019
Look in the File Exchange. There are several thousand example programs there for various purposes. You could study some of them to get a better idea of how MATLAB programs are structured and to see real examples of how people use cell array. You can also see good examples of documentation, people explaining the requirements of a task and working through the steps towards a solution.
Rik on 14 Jun 2020
My reason for closing this question: people are just posting their answers to this homework question, without substantial discussion happening. Once there is an option to disallow new answers while allowing comments that option should be considered for this thread.

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

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
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

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.
Remie Matta on 23 Jun 2020
where did e and l come from?
Rik on 23 Jun 2020
They are created on the third line of this function. Have you read the documentation for the size function?

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;

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
Rik on 22 May 2020
If you are going to post a complete solution to a homework question, at least write proper documentation and comments for your function, and make sure to properly allign the code.

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
Walter Roberson on 8 Jun 2020
What benefit do you see for incrementing i within the for i loop?

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

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
Walter Roberson on 10 Jun 2020
Suppose the data indicates that the matrix is to be 3 x 4. Then you would do
for i = 1 : 3; for j = 1 : 4; sparse_matrix(i,j) = a{1,2}; end; end
The first iteration, sparse_matrix is a 1 x 1 scalar, 0, and a 1 x 1 scalar is the same as indexing at (1,1), so assigning to sparse_matrix(1,1) is re-using the storage that had the 0 and that is fine.
The second iteration, sparse_matrix is a 1 x 1 scalar, and you assign to sparse_matrix(1,2) but that is larger than the existing matrix. MATLAB deals with that by creating a new temporary matrix that is large enough to hold the existing data and new data (so 1 x 2) and then copies the existing data into it (so (1,1) would be copied into the temporary matrix); then releases the old matrix and replaces the pointers to it with the new 1 x 2 matrix, and then it does the assignment of the value into the now-larger matrix putting it into location (1,2) .
The third iteration, sparse_matrix is 1 x 2, and you assign to (1,3) but that is larger than the existing matrix. A new larger tempory matrix that is 1 x 3 has to be built, the existing 1 x 2 is copied into it, the 1 x 2 is released, the new value is copied into (1,3).
The fourth iteration, sparse_matrix is 1 x 3, you assign to (1,4), new tempory 1 x 4 is created, existing 1 x 3 is copied into it, old one is released, (1,4) is assigned into.
That is the last j iteration; then you move on to the second i iteration, so i=2, j=1 . sparse_matrix(2,1) is being assigned into, but existing is 1 x 4, so a complete second row needs to be allocated making a 2 x 4 , the existing 1 x 4 copied into it, the 1 x 4 released, then (2,1) is updated.
Next is i=2, j=2. This time the array is already 2 x 4, so no growing is needed, so (2,2) gets updated directly. Same for (2,3) and (2,4).
Then at i=3, j=1, once more you have to allocate a complete new row, copying the existing 2 x 4 into it, release old one, assign into the new (3,1). Then for i=3, j=2 to 4 no new allocation is needed.
So when you assign into a matrix in this way, the matrix ends up getting grown with all the existing items having to be copied, once for each column (for the first row), and then once again for each new row added. That requires copying 1 + 2 + 3 + ... cols = cols * (cols+1) / 2 the first time around. Then each additional row requires copying (row_number-1)*cols items, and by the end of rows that would be rows * (rows-1)/2 * cols items copied around. For 50 rows and 30 columns that would add up to having to copy around about 37215 items. For 100 rows and 30 columns it would be about 148965 data items copied.
Now on the other hand if you were to initialize sparse_array to its known final size, then no copying around would be needed at all.
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

R2016b

### Community Treasure Hunt

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

Start Hunting!