receiving a sparse matrix
2 次查看(过去 30 天)
显示 更早的评论
I have a problem with creating a sparse matrix.I readed the explanation from MATLAB help some number of times but can't understand how exactly to do it and which parameter what means. I want to receive a sparse matrix with column size 10000, and using only one matrix of cells with size 1x40.Every cells are again vectors(lists) with different lenght (1:x,(x<10000)) and contain a some values. I want to make a sparse matrix with size 40x10000 and inside this matrix to position the values from the cell.Every row of the output matrix correspond to 1 cell from the input vector(1x40).
Can somebody help me, please!!!
0 个评论
采纳的回答
Sarah Wait Zaranek
2011-5-4
If I understand you correctly, what you want to do is take your cell array (1 x 40) - and for each column of the cell array maps those values to part of your sparse matrix.
The easiest way to do this would be using the sparse command. You probably would need to do a bit of set up to get the correct i and j values. This is one way to do it.
% creating general data to match your format
x = floor(10000*rand(40,1));
myData = arrayfun(@(y) [1:y ; rand(1,y)],x , 'UniformOutput', false)';
% This is where you start
% creating ii values
b = num2cell(1:length(x));
a = cellfun(@(xx,bb) bb.*ones(1,length(xx)) , myData, b, 'UniformOutput',false);
% extracting jj and element values
ii = [a{:}];
myDataMat = [myData{:}];
% creating your sparse matrix
yourMatrix = sparse(ii,myDataMat(1,:),myDataMat(2,:),40,10000);
2 个评论
Sarah Wait Zaranek
2011-5-6
No problem.
So - let me go bit and bit, and see if that helps clear up what I am doing. Once that makes sense, I think it will be easy for you to modify it for alternative uses.
The first few lines are just me generating data that looks like yours, my x vector represents the length of the data in each column of your cell array. I then use the arrayfun function which operates a given function on each element of the input array. In this case, my input array is x, and the function is given as an aanonymous function. If you haven't used anonymousfunctions before, they are really useful. There is a nice section in the MATLAB documentation about them here:
http://www.mathworks.com/help/releases/R2011a/techdoc/matlab_prog/f4-70115.html#f4-70133
This gives me a starting cell array with the same stucture as your data.
Ultimately, I want to call sparse to create my sparse array. The inputs should be 3 vectors (the row indices, the column indices, and the value at that location) and the size of the matrix (40,10000 in your case).
The column indices are already contained in the cell array, as are the values so those are pretty straightforward to extract. The only trick is that I need to index into the cell array and have all the resulting values gathered together in one array. The easiest way to do that is something like: [yourcell(:,1)] . I do that to get a vector representing the row indices and the values.
Does that make sense so far?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!