repeat rows and export to txt file

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 commentaires

A=[1;2;3;4];
X=3;
k=1;
for i=1:length(A)
for j=1:X
B(k)=cat(1,A(i));
k=k+1;
end
end
%A=B incase you need result back in orignal Varaible
Jacob Weiss
Jacob Weiss le 8 Juil 2021
The file consists of 16120 rows and 4 columns. I need to import and export the new txt file. I.E
txt file:
1 2 3 4
4 5 6 7
.......
then repeat the rows 8 times each, one after another, so that the new file is 128960 rows long. And finally export it to a txt file which is not mentioned in the comment above. Thank you.
Mohsin Zubair
Mohsin Zubair le 8 Juil 2021
@Jacob Weiss I told you that how can you create your desired output but to export it, there are many ways to do so, it depends how or in whih format you want to export your data, also can you share your orignal text file with data?
Mohsin Zubair
Mohsin Zubair le 8 Juil 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

Connectez-vous pour commenter.

Réponses (1)

Scott MacKenzie
Scott MacKenzie le 8 Juil 2021
Modifié(e) : Scott MacKenzie le 8 Juil 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 commentaires

Jacob Weiss
Jacob Weiss le 8 Juil 2021
I get a 3x1 struct type if I use the code where x=8 and size(A,4) because its 4x16120. Should A1 = repmat(A', x, 1); be A1 = repmat(A', x, 4)?
Jacob Weiss
Jacob Weiss le 8 Juil 2021
I attached the file I would like to repeat and export
Scott MacKenzie
Scott MacKenzie le 8 Juil 2021
Modifié(e) : Scott MacKenzie le 8 Juil 2021
Unless I've missed something, the key aspect of your question is repeating each row in the data x times. The input file you just posted contains a 16120x4 matrix of data. So, here's my answer with the addition of reading and writing to and from files:
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/678488/Raj_AZ31_16120.txt';
A = readmatrix(f);
x = 3; % number of times to repeat each row (change as desired)
A1 = repmat(A', x, 1);
A2 = reshape(A1, size(A,2), [])';
writematrix(A1,'newfile.txt');
I ran this on my computer and it created the desired output file with the data in a 48360x4 matrix
Jacob Weiss
Jacob Weiss le 9 Juil 2021
Awesome it worked thank you!
Jacob Weiss
Jacob Weiss le 9 Juil 2021
However it is not spaced properly and it does not create new rows just adds onto the previous row. I attached the file for refrence.
Oops. Minor (major?) typo in my script. Change the last line from
writematrix(A1,'newfile.txt'); % Oops, wrong matrix written to file!
to
writematrix(A2,'newfile.txt'); % write the A2 matrix to file
You might want to consider repelem instead of repmat:
A=[1;2;3;4];
repelem(A,3,1)
ans = 12×1
1 1 1 2 2 2 3 3 3 4
@Rik. Thanks. Yes indeed, much simpler. @Jacob Weiss with Rik's simplification, something like this will also work...
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/678488/Raj_AZ31_16120.txt';
A = readmatrix(f);
x = 3; % number of times to repeat each row (change as desired)
B = repelem(A, x, 1);
writematrix(B,'newfile.txt');
Jacob Weiss
Jacob Weiss le 9 Juil 2021
Great thank you for all the help! I have it working now.

Connectez-vous pour commenter.

Commenté :

le 9 Juil 2021

Community Treasure Hunt

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

Start Hunting!

Translated by