how to write this data into a sparse matrix

Dear All,
I have this data which is represent 0,1 matrix. it is given in this way. the first element 12,4,3,18,19,10 means the number of position of the ones in this row. I want to create a sparse matrix for it. can anyone help me with it?
12 1 2 3 4 60 61 62 63 89 90 91 95
4 32 33 34 35
3 83 84 91
18 39 40 41 42 43 71 72 73 74 75 76 77 88 90 92 93 94 95
19 29 30 31 32 33 34 35 36 37 79 80 81 83 84 85 86 87 88 89
10 73 86 87 88 89 90 91 92 93 94
the final matrix should have size 6x95. I appreciate any help.
regards,
Nadia

4 Commenti

How are these input data stored now? For example, do you have six different vectors with these values? How are they named?
basically, they are a binary matrix the information wrote in this way in the attach file and I should create the whole matrix. the problem some matrices are size 3000so it will be difficult to arrange by hand.
thanks for replying
Please edit your question and upload your exact data: click the paperclip button, then both Choose file and Attach file buttons.
Stephen23
Stephen23 il 29 Feb 2016
Modificato: Stephen23 il 29 Feb 2016
How did you get all of these lines of numbers into this text file? Did you copy-and-paste them from some source? If so, what source? It may be easier to simply get MATLAB to read the data directly from that source.
The answer really depends on you: do you want a general solution that will also work for other data, or are you wanting to process only this data?

Accedi per commentare.

 Risposta accettata

columns = {[12 1 2 3 4 60 61 62 63 89 90 91 95]
[4 32 33 34 35]
[3 83 84 91]
[18 39 40 41 42 43 71 72 73 74 75 76 77 88 90 92 93 94 95]
[19 29 30 31 32 33 34 35 36 37 79 80 81 83 84 85 86 87 88 89]
[10 73 86 87 88 89 90 91 92 93 94]}
rows = arrayfun(@(row) repmat(row, 1, numel(columns{row})), 1:numel(columns), 'UniformOutput', false);
out = sparse([rows{:}], [columns{:}], 1)

7 Commenti

Dear Guillaume,
thanks for replying, should I write [ ] because I have about 3000 line of numbers. also the first elements they just mentioned the number of positions we don't need to write it in the matrix.
thanks again
nadia nadi
nadia nadi il 29 Feb 2016
Modificato: nadia nadi il 29 Feb 2016
Dear,
i'm sorry I thought I sent the data, I sent it now. you can see i have no problem with the dimensions n,m and the coefficients row. but with other lines for example
12 1 2 3 4 60 61 62 63 89 90 91 95
where the first number 12 represents the number of ones I should put in there positions 1 2 3 4 60 61 62 63 89 90 91 95. the sparse matrix should be 100x95. I don't need to put ones for the first number in each row 12,4,3,18,19,10. I hope is clear now and I'm sorry again for the misunderstand.
regards,
Guillaume
Guillaume il 29 Feb 2016
Modificato: Guillaume il 29 Feb 2016
Since you didn't tell us how your data was stored, I took a guess it was in cell array as it's the most logical way of storing it. However, it doesn't appear you've even reach the stage of importing your data in matlab.
So where is it at present? And more importantly, is it all on one line or is it on several lines as in your example?
Assuming your data is in a text file where each row is on a different line, you can import it into matlab with:
filecontent = fileread('C:\full\path\to\your\text file.txt');
columns = cellfun(@str2num, strsplit(filecontent, {'\n', '\r'}), 'UniformOutput', false);
This should give you a cell array similar to the one in my answer.
You actually don't need the first number of each row, we can simply count the number of numbers (sic) in each row. Modified code to ignore that first number:
%remove the 1st number in each row
columns = cellfun(@(c) c(2:end), columns, 'UniformOutput', false);
%same code as before
rows = arrayfun(@(row) repmat(row, 1, numel(columns{row})),
1:numel(columns), 'UniformOutput', false);
out = sparse([rows{:}], [columns{:}], 1)
dear Guillaume,
thanks, I thought you read the attachment this is the data. I saved the data in matlab file not text file because I want to use fprintf but my data are not in a matrix. I believe all what you suggested is ok if I can save it in txt file.
Yes, I saw your m file. This is not the origin of your data obviously. That m file is not very useful as it can't be interpreted by matlab.
The simplest thing is for you to edit that file to remove the first row and edit the 2nd row so it's just the numbers. Once you've done that you can then import the data into matlab with the code I've given:
filecontent = fileread('data.m');
columns = cellfun(@str2num, strsplit(filecontent, {'\n', '\r'}), 'UniformOutput', false);
%remove the 1st number in each row
columns = cellfun(@(c) c(2:end), columns, 'UniformOutput', false);
%same code as before
rows = arrayfun(@(row) repmat(row, 1, numel(columns{row})),
1:numel(columns), 'UniformOutput', false);
out = sparse([rows{:}], [columns{:}], 1)
Note that I would change the extension of the file to '.txt' as it's not a proper m file, but that does not matter for the code.
because I want to use fprintf: that's totally irrelevant. Importing the data is a different task from displaying it.
That's great Guillaume,
thanks for keeping work with me until you sorted it. many thanks
   

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by