receiving a sparse matrix

2 visualizaciones (últimos 30 días)
Hristo Alexiev
Hristo Alexiev el 4 de Mayo de 2011
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!!!

Respuesta aceptada

Sarah Wait Zaranek
Sarah Wait Zaranek el 4 de Mayo de 2011
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 comentarios
Hristo Alexiev
Hristo Alexiev el 6 de Mayo de 2011
Thank you very much. I can't understand the entire code,but it's working :). I want to decide another problem. What will change (or how will look like the code) if the values in every cell (in every list) corresponds to the indexes of the column in the sparse matrix,and if I want to use these indexes to put a 1 in the resulting sparse matrix.So that if some value from the cells (some index in the resulting sparse matrix) is repeat, the algorithm to make increasing of the value written in the sparse matrix (something like counting of the values wich are repeating and writing their number in the sparse matrix of the place with index,row-corresponding to the cell(1-40) and column corresponding to the value written in the elements of the list).
Thank you in advance.
:)
P.S. And sorry for the mistake in the layout of the answers.
Sarah Wait Zaranek
Sarah Wait Zaranek el 6 de Mayo de 2011
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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by