# Sparse Matrix, struct, structure, Using cell arrays

34 visualizzazioni (ultimi 30 giorni)
Abhishek Gupta il 30 Ott 2020
Risposto: Abhishek Gupta il 30 Ott 2020
A sparse matrix is a large Matrix with almost all elements of the same value (typically zero). The normal representation of a spark Matrix takes up lots of memory when 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 sparse matrix that has a value other than default. The three elements 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Abhishek Gupta il 30 Ott 2020
function matrix=sparse2matrix(cellvec)
matrix=cellvec{1,2}*ones(cellvec{1,1});
[m n]=size(cellvec);
for i=3:n
A=cellvec{1,i};
p=A(1);
q=A(2);
matrix(p,q)=A(3);
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

Aramis il 7 Mag 2024
function matrix = sparse2matrix(cellvec)
matrix = ones(cellvec{1}) * cellvec{2};
for x=3:length(cellvec)
matrix(cellvec{x}(1),cellvec{x}(2)) = cellvec{x}(3);
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by