Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Assignment support in Sparse Matrix

1 回表示 (過去 30 日間)
Tashu Bar
Tashu Bar 2019 年 3 月 17 日
閉鎖済み: John D'Errico 2019 年 3 月 23 日
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 件のコメント
paul mary
paul mary 2019 年 3 月 21 日
Hello, did you figure it out? I have same problem.

採用された回答

John D'Errico
John D'Errico 2019 年 3 月 17 日
編集済み: John D'Errico 2019 年 3 月 17 日
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 件のコメント
Priyamvada Shankar
Priyamvada Shankar 2019 年 3 月 23 日
Please help...
John D'Errico
John D'Errico 2019 年 3 月 23 日
編集済み: John D'Errico 2019 年 3 月 23 日
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...

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by