Reshape multiple dat files

2 次查看(过去 30 天)
Hello all,
I have a large number of dat files in which multiple variables are arranged in a single column one after the other. Each variable is separated by several -9.
Like so: 1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12 13 ...
Now, I want to reshape them so I have one variable per column and get rid of the -9. Like so:
A B C
1 5 9
2 6 10
3 7 11
4 8 12
Please note that the number of variables, their length and the number of -9 between them may change.
Any ideas ?

采纳的回答

Star Strider
Star Strider 2017-6-5
One approach:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12];
Vr = V;
Vr(Vr==-9) = [];
Vr = reshape(Vr(:), [], 3)
VrT = table(Vr(:,1),Vr(:,2),Vr(:,3), 'VariableNames',{'A','B','C'})
Vr =
1 5 9
2 6 10
3 7 11
4 8 12
VrT =
4×3 table
A B C
_ _ __
1 5 9
2 6 10
3 7 11
4 8 12

更多回答(1 个)

Guillaume
Guillaume 2017-6-5
编辑:Guillaume 2017-6-5
I'm assuming that within the same file, the number of -9 between each variable is constant:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12]' %column vector as stated
nines = find(V == -9); %location of all nines
startnines = nines(diff([0; nines]) > 1); %start locations of -9 sequences
endnines = nines(diff([nines; Inf]) > 1); %end locations of -9 sequences
Vr = [V; repmat(-9, endnines(1) - startnines(1) + 1, 1)]; %append -9 after last variable
Vr = reshape(Vr, endnines(1), []); %reshape into columns of variables
Vr(startnines(1):endnines(1), :) = [] %delete -9
edit: Actually it's not much more complicated if the number of -9 between each variable is not even constant within the same file:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12]' %column vector as stated
nines = find(V == -9); %location of all nines
startnines = nines(diff([0; nines]) > 1); %start locations of -9 sequences
endnines = nines(diff([nines; Inf]) > 1); %end locations of -9 sequences
seqlengths = diff([1; reshape([startnines, endnines+1]', [], 1); numel(V)+1]);
Vr = mat2cell(V, seqlengths, 1);
Vr = [Vr{1:2:end}]

类别

Help CenterFile Exchange 中查找有关 Genomics and Next Generation Sequencing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by