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