Reshape multiple dat files

조회 수: 2 (최근 30일)
012786534
012786534 2017년 6월 5일
편집: Guillaume 2017년 6월 5일
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