필터 지우기
필터 지우기

how can transform a matrix into file .txt??

조회 수: 1 (최근 30일)
Sahar abdalah
Sahar abdalah 2015년 5월 20일
댓글: Star Strider 2015년 5월 22일
hello everyone, I have a matrix proba (size :10 * 5).
proba=[0.5 0.3 0.8 0.9 0.8;
0.50 0.36 0.58 0.58 0.98;
0.1 0.25 0.6 0.8 0.9;
0.5 0.3 0.8 0.9 0.8;
0.2 0.9 0.58 0.58 0.69;
0.58 0.14 0.1 0.2 0.3;
0.25 0.9 0.8 0.7 0.5;
0.58 0.69 0.25 0.1 0.1;
0.1 0.25 0.36 0.2 0.3;
0.5 0.3 0.8 0.9 0.8 ];
I want to transform this matrix into a text file (proba.txt) with which the index column is written and the value of the column for each line as follows :
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
1 0.50 2 0.36 3 0.58 4 0.58 5 0.98
.
.
.
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
Please I need help, how can I do it?thanks in advance

채택된 답변

Star Strider
Star Strider 2015년 5월 20일
This will do what you want:
q(:,2:2:size(proba,2)*2) = proba; % Create Output Matrix
q(:,1:2:size(q,2)) = repmat([1:size(proba,2)], size(q,1), 1); % Create Output Matrix
probatxt = sprintf([repmat('% f', 1, size(q,2)) '\n'], q') % Test
fidout = fopen('proba.txt','w');
fprintf(fidout, [repmat('% f', 1, size(q,2)) '\n'], q')
fclose(fidout)
It first creates an intermediate array ‘q’ from ‘proba’ with its elements in alternate columns, then fills the remaining columns with the numbered column indices. (The sprintf call just displays the output as it will appear in the text file, and is not necessary for the code.) It then creates the file and writes to it.
  댓글 수: 2
Sahar abdalah
Sahar abdalah 2015년 5월 22일
thanks
Star Strider
Star Strider 2015년 5월 22일
My pleasure.

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

추가 답변 (1개)

Christiaan
Christiaan 2015년 5월 20일
Dear Sarah,
In this link you find the answer for that.
Good luck! Christiaan

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by