repeat rows and export to txt file

1 view (last 30 days)
Jacob Weiss
Jacob Weiss on 8 Jul 2021
Commented: Jacob Weiss on 9 Jul 2021
I read in a txt file containing n colums and n rows. I need to repeat each row x number of times and then export the new file to a txt document. I.E
A=importdata('file.txt')
A = [1;2;3;4]
x= 3
A = [1;1;1;2;2;2;3;3;3;4;4;4]
exportdata('NewFile.txt')
  4 Comments
Mohsin Zubair
Mohsin Zubair on 8 Jul 2021
well what I told is just using loops which isn't good for long file, what scott told you is much better for file of your size

Sign in to comment.

Answers (1)

Scott MacKenzie
Scott MacKenzie on 8 Jul 2021
Edited: Scott MacKenzie on 8 Jul 2021
I think this is more or less what you're after:
% test data (read from file)
A = [1 8;2 7;3 6;4 5]
A = 4×2
1 8 2 7 3 6 4 5
x = 3; % number of times to repeat each row (change as desired)
A1 = repmat(A', x, 1);
A2 = reshape(A1, size(A,2), [])'
A2 = 12×2
1 8 1 8 1 8 2 7 2 7 2 7 3 6 3 6 3 6 4 5
% write to file
  9 Comments
Jacob Weiss
Jacob Weiss on 9 Jul 2021
Great thank you for all the help! I have it working now.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!