필터 지우기
필터 지우기

how to write this data into a sparse matrix

조회 수: 1 (최근 30일)
nadia nadi
nadia nadi 2016년 2월 29일
댓글: nadia nadi 2016년 2월 29일
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
Stephen23
Stephen23 2016년 2월 29일
Please edit your question and upload your exact data: click the paperclip button, then both Choose file and Attach file buttons.
Stephen23
Stephen23 2016년 2월 29일
편집: Stephen23 2016년 2월 29일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 2월 29일
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
Guillaume
Guillaume 2016년 2월 29일
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.
nadia nadi
nadia nadi 2016년 2월 29일
That's great Guillaume,
thanks for keeping work with me until you sorted it. many thanks
   

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by