Assignment support in Sparse Matrix

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:
Can somebody guide , have tried but was not able to understand how to attempt this problem.
Help is appreciated

1 Comment

Hello, did you figure it out? I have same problem.

 Accepted Answer

John D'Errico
John D'Errico on 17 Mar 2019
Edited: John D'Errico on 17 Mar 2019
Answers is not a homework service, so I will not do your homework for you. However, this question is one that I would argue is confusing or perhaps misleading - because it tries to make you think along the lines of sparse matrices, when no sparse matrix will be constructed. So, what is being requested?
  1. Can you construct an nxm matrix that contains entirely a specific element? I.e., what is the default element? I hope you can do so by now. Is that not what you are told to do by the first two arguments?
  2. Can you then replace specific elements of that matrix? Even if a simple loop is required, there is no reason why this is not simple to do. (You might need to know how to use varargin, since there are an unspecified number of input arguments here.)
So what is the problem? Is that not all you were told to do? Could you have done this in a more efficient way than a loop? Well, yes, by a judicious use of cell2mat and sub2ind. In fact, you could have used sparse itself, or you could use accumarray. Are those vectorized alternatives pertinent? No.
When you are given a question, look at what is required. Try to ignore the extraneous information that serves only to mislead you. This is a classicly important part of any problem solving effort.

8 Comments

This is not an answer to the question. It is not even correct. Use comments instead to comment. Answer moved to a comment by Tashu Bar:
Please find below
function [matrix]=sparse2matrix(cellvec)
% Matrix definition
cell=cell2mat(cellvec);
row=cell(1);
column=cell(2);
default=cell(3);
first_elem_row_location=cell(4);
first_elem_column_location=cell(5);
first_elem_value=cell(6);
second_elem_row_location=cell(7);
second_elem_column_location=cell(8);
second_elem_value=cell(9);
%-------------------------------------------------------------------------------
A=zeros(row,column);
A(first_elem_row_location,first_elem_column_location)=first_elem_value;
A(second_elem_row_location,second_elem_column_location)=second_elem_value;
matrix=A;
Well, it is some effort in the right direction. Not a good choice of direction, because you will not be able to extend to to allow more elements easily. But having shown some effort, I'll see if I can steer you into a solution.
In the example, we saw
cellvec = {[2 3],0,[1 2 3],[2 2 -3]};
This is an array that contains 4 elements. Not 9. You don't want to turn it into a vector of elements, nor do you need to do so. So use those elements as I instructed. We know what the first two elements of cellvec will always be.
I'll get you started:
function [matrix]=sparse2matrix(cellvec)
% Matrix initialization
msize = cellvec{1};
mdef = cellvec{2};
matrix = repmat(mdef,msize);
% now loop over each element of cellvec beyond 2,
% to fill in each element, one at a time.
for n = 3:numel(cellvec)
% RCV = row, column, value
RCV = cellvec{n};
% fill in the rest here:
end
So, at each step of that for loop, RCV is now a vector of length 3. What will you do with it? What do the elements of RCV mean here? How can you use them?
The above code, with a loop that you need to finish writing, is a simple solution to the problem at hand. However, at some point in your programming career, you might decide that you can do the latter part where I had a loop more efficiently. At that point, you might consider a code fragment like this:
reshape(cell2mat(cellvec(3:end)),[],3)
Think about what that will do. Even simpler, we might think along these lines:
vertcat(cellvec{3:end})
In either case, look at the result. Now, could you use a tool like accumarray? (In fact, you could essentially write the entire solution to this problem in one or two lines, IF you use accumarray properly. But that is not pertinent to the problem, where any solution is sufficient.)
Can you please write whole code... I'm completely stuck
John has already written pretty much the entire code for you except one line ("... fill in the rest here..."). Can't you figure out that one line? Given the RCV vector, how would you use that to assign one element of the matrix?
Tashu Bar
Tashu Bar on 23 Mar 2019
Edited: Tashu Bar on 23 Mar 2019
Thank you John D'Errico for your guidance and inspiration , you are a good teacher.
I could do it in line you indicated
John D'Errico
John D'Errico on 23 Mar 2019
Edited: John D'Errico on 23 Mar 2019
I am sorry. But you need to put in some effort in this. It is your homework, not mine. This is how you learn. Not by us giving you the answer you can then turn in, without any investment of thought.
You need to write one line of code to finish what I started. I even gave you a big hint in the form of the name of the vector RCV.
John D'Errico
John D'Errico on 23 Mar 2019
Edited: John D'Errico on 23 Mar 2019
In my comment, I've given you all but one moderately simple line of code that you need to write. Look to see what the variable RCV is in that code, inside the loop. Think about it.
Row index. Column index, Value
So, what would RCV(1) represent? Do I need to make this more clear yet? If you are interested in learning MATLAB, then you will make an effort. If you are not, and want only someone to fully write your homework assignment, then I must be done with this.
So. One line of code. What will it be? Please try...

More Answers (0)

This question is closed.

Asked:

on 17 Mar 2019

Closed:

on 23 Mar 2019

Community Treasure Hunt

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

Start Hunting!