Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

# 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 visualizzazione (ultimi 30 giorni)
Abhishek singh il 21 Apr 2019
Chiuso: Rik il 23 Giu 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..
##### 6 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
Rik il 14 Giu 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.

### Risposte (10)

Arafat Roney il 11 Mag 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 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Emine Ertugrul il 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 CommentoMostra NessunoNascondi Nessuno
Rik il 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 il 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 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Soham Khadilkar il 26 Apr 2020
Modificato: Soham Khadilkar il 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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Rik il 23 Giu 2020
They are created on the third line of this function. Have you read the documentation for the size function?

Ved Prakash il 15 Mag 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 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Syed Zuhair Ali Razvi il 22 Mag 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 CommentoMostra NessunoNascondi Nessuno
Rik il 22 Mag 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 il 3 Giu 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 CommentoMostra NessunoNascondi Nessuno
Walter Roberson il 8 Giu 2020
What benefit do you see for incrementing i within the for i loop?

Raymond He il 8 Giu 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 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

UJJWAL Padha il 10 Giu 2020
Modificato: UJJWAL Padha il 10 Giu 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 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
UJJWAL Padha il 11 Giu 2020
i understand now.....
Thankyou for sharing this piece of knowledge....
Appreciate it :)

Md Nazmus Sakib il 19 Giu 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!