Generating Randomization from a text file

조회 수: 6 (최근 30일)
Mahmoud Khadijeh
Mahmoud Khadijeh 2019년 9월 1일
댓글: Joseph Kutteh 2021년 6월 8일
Hello,
I have a text file contains numbers like the following :
1 2 3
7 5 6
4 5 5
9 11 1
I want to redistribute the lines randomly to be for example like this:
7 5 6
9 11 1
4 5 5
1 2 3
I know about the function " randperm " but I want to use it in case I have a text file and I want to generate the randomization inside it.
Regards,
  댓글 수: 1
Jan
Jan 2019년 9월 4일
What is your question? Do you know how to import the data from a file to a matrix? Do you know how to shuffle the rows? Does the problem include how to write a matrix to a text file? What have you tried so far?

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

채택된 답변

Sai Sri Pathuri
Sai Sri Pathuri 2019년 9월 4일
You can read the matrix from the text file, randomize the rows and rewrite it to the text file.
For reading a matrix from text file, you may use readmatrix function.
a=readmatrix('filename.txt');
To shuffle the rows, you may use the following code.
b=a(randperm(size(a,1)),:);
To write matrix to the text file, use writematrix function.
writematrix(b,'filename.txt','Delimiter','tab');
  댓글 수: 2
Mahmoud Khadijeh
Mahmoud Khadijeh 2019년 9월 6일
Thank you very much
Joseph Kutteh
Joseph Kutteh 2021년 6월 8일
Works great! Thank you as well.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by