# sparse2matrix random error

7 visualizzazioni (ultimi 30 giorni)
Abhishek singh il 25 Apr 2019
Modificato: Milad Mehrnia il 24 Ott 2021
function [matrix]= sparse2matrix(incell);
X=size(incell);
q=X(2)-2;
msize=incell{1};
mdef=incell{2};
matrix=repmat(mdef,msize);
while q > 0
msize= size(matrix);
matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);
q = q-1 ;
end
Variable solution has an incorrect value.
sparse2matrix( { [ 6 11 ], 9, [ 4 11 4 ], [ 2 8 0 ], [ 4 9 7 ], [ 2 7 8 ], [ 5 5 -7 ], [ 3 10 6 ], [ 2 11 8 ],
[ 2 5 -7 ], [ 2 8 -4 ], [ 5 7 3 ] } )
failed...
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
dpb il 25 Apr 2019
Not enough context...what's the problem?
Abhishek singh il 25 Apr 2019
getting error for this input
matrix = sparse ( { [ 6 11 ], 9, [ 4 11 4 ], [ 2 8 0 ], [ 4 9 7 ], [ 2 7 8 ], [ 5 5 -7 ], [ 3 10 6 ], [ 2 11 8 ],
[ 2 5 -7 ], [ 2 8 -4 ], [ 5 7 3 ] } )

Accedi per commentare.

### Risposte (9)

Prathiksha RD il 25 Set 2020
%This function is a bit bigger but it is just to help you guys to understand better
%This function is to get a sparse matrix
function matrix = sparse2matrix(cellvec)
row = cellvec{1}(1);
col = cellvec{1}(2);
a = cellvec{2};
matrix = a * ones(row,col); %This is to obtain the initial matrix with the desired number
for ii = 3: length(cellvec)
r = cellvec{ii}(1,1);
c = cellvec{ii}(1,2);
n = cellvec{ii}(1,3);
matrix(r,c) = n;
end
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Michelle Zheng il 4 Dic 2020
Thank you for this answer! It was actually really helpful to for you to break it down step by step, especially since I didn't really understand the question at first.

Accedi per commentare.

Wilver Sánchez il 11 Feb 2020
%
function matrix=sparse2matrix(cell)
M=cell{2}*ones(cell{1}(1,1),cell{1}(1,2));
for ii=1:length(cell)-2
M(cell{2+ii}(1,1),cell{2+ii}(1,2))=cell{2+ii}(1,3);
end
matrix=M;
end
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
saiyad junaid il 18 Mag 2020
Great it worked!!!
but what this line means
M=cell{2}*ones(cell{1}(1,1),cell{1}(1,2));
and please explain some further lines too. Grateful for this kardes!!!
TANUJA GUPTA il 31 Lug 2020
Modificato: TANUJA GUPTA il 31 Lug 2020
M = cell{2}* ones(cell{1}(1,1), cell{1}(1,2));
This statement means creating a new vector; M, which is a product of the second element of the cell vector and an identity matrix of size row = 1st element's 1st value and column = 1st elements 2nd value.

Accedi per commentare.

James Tursa il 25 Apr 2019
You have two entries for the (2,8) spot, namely [2 8 0] and [2 8 -4]. The way you have your algorithm coded, the first one in the list will prevail. If you want different behavior you will have to change your code, but you haven't told us the rules for this situation. Do you want the last one to prevail? Do you want to add the values for these cases? Or ...?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

KOUSHIK NANDY il 3 Mag 2019
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Walter Roberson il 3 Mag 2019
The solution has been posted in other questions on the same topic. However since this is an assignment then using the solution could risk plagiarism.

Accedi per commentare.

TANUJA GUPTA il 31 Lug 2020
% This function is to get a sparse matrix
% The first element's first value represent the size of the sparse matrix, whereas the first element's
% second value represent the column of the sparse matrix. The second element of the vector represent the
% default value of the sparse matrix.
% When we come to the third element of vector matrix, the first value of the element represent the rows
% and column of that matrix; whereas the third element represent the actual value of that location.
% The representation of next vector element is similar to that of the previous one
function new_matrix = sparse2matrix(vector) % function definition
sparse_row = vector{1}(1); % assignment of no of rows to the vectors 1'st elements first value.
sparse_column = vector{1}(2); % assignment of no of columns to the vectors 1'st elements second value
new_matrix = vector{2}*ones(sparse_row,sparse_column);%This will create a new vector, which is obtained
% by multiplying the value of first vector to the identity matrix of size
% i.e. of sparse matrix. Thus generating a new vector which is of same size
% as sparse matrix. in other words we are preallocating the elements to the
% new vector, with default value as 0.
for i= 3:length(vector) % This helps in reaching to the last element of the vector form the 3rd element
new_matrix(vector{i}(1),vector{i}(2))= vector{i}(3); % giving the values to the new vector
% which were intitially 0.
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Aakash Kotia il 8 Ago 2020
function [matrix]= sparse2matrix(cell)
s=cell{2}(1,1)*ones(cell{1}(1,1),cell{1}(1,2));
for i=3:length(cell)
s(cell{i}(1,1),cell{i}(1,2))=cell{i}(1,3);
end
matrix=s;
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Arifuzzaman Joy il 14 Feb 2021
function matrix = sparse2matrix(cellvec)
a=cellvec{1,1};
z=zeros(a(1,1),a(1,2))++cellvec{1,2};
for ii=3:(numel(cellvec))
a=cellvec{1,ii};
z(a(1,1),a(1,2))=a(1,3);
end
matrix=z;
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Nguyen Bui il 7 Giu 2021
function maxtrix = sparse2matrix(cellvec)
a = cellvec{1};
maxtrix = cellvec{2}*ones(a(1,1),a(1,2));
for ii = 3:length(cellvec)
maxtrix(cellvec{ii}(1,1),cellvec{ii}(1,2)) = cellvec{ii}(1,3);
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Milad Mehrnia il 24 Ott 2021
Modificato: Milad Mehrnia il 24 Ott 2021
function matrix = sparse2matrix(c) % c = cellvec
matrix = ones(c{1}).*c{2};
for i = 3:length(c)
p = c{i}(1:2);
matrix(p(1),p(2)) = c{i}(end);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

R2015b

### Community Treasure Hunt

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

Start Hunting!

Translated by