File data with columns arranged with different number of rows.

19 views (last 30 days)
I need to write data which have pairs of columns with different number of rows.
Is it possible? Because I tried to create it, but there are errors with cat and horzcat, I understand that is for the weird size of matrix.
I need to have the data in the same file. Is it possible?
One file for three matrices:
[A(mx2) B(nx2) C(px2)]
I need this file to use in LaTeX as coordinates. I want that LaTeX reads the file in pair of columns. I don't know if it is possible. Can someone that can elucidate this issue, please.
c1 c2 c3 c4
1 2 8 1
2 2 3 6
1 5 7 3
0 5 7 9
2 1 8 2
3 0 1 4
7 8
3 1
9 2
In order to allow LaTeX reads as coordinates, first (c1,c2), then (c3,c4).
Or maybe there is another solution, because I have various matrices to use with big data.
  1 Comment
Bob Thompson
Bob Thompson on 2 May 2018
Can you just use vertcat()? Since all the matrices have the same number of columns you just append the different rows together.
data = vertcat(A B C);
Or you should be able to use semicolons to indicate vertical concatenation.
data = [A; B; C];

Sign in to comment.

Accepted Answer

dpb
dpb on 4 May 2018
Edited: dpb on 4 May 2018
With the assumption of just three specific arrays, the last idea--
a=length(A);b=length(B);c=length(C); % length each (use, size,1)
R=max([a b c]); % longest to augment to
D=[[A;nan(R-a,2)] [B;nan(R-b,2)] [C;nan(R-c,2)]]; % build total array
D=cellstr(num2str(D)); % turn to cell string for
D=char(strrep(D,'NaN',' ')); % replacement/back to char
fid=fopen('out.txt','w'); % ready to write to file
for i=1:R % by record
fprintf(fid,'%s\n',D(i,:)); % string follow by \n
end
fid=fclose(fid);
>> type out.txt % see what we gots...
2 2 2 2 2 2
1 5 1 5 1 5
0 5 0 5 0 5
2 1 2 1 2 1
3 0 3 0
7 8 7 8
3 1 3 1
9 2 9 2
2 2
1 5
0 5
2 1
3 0
2 2
1 5
0 5
2 1
3 0
7 8
3 1
9 2
>>

More Answers (1)

dpb
dpb on 2 May 2018
Edited: dpb on 2 May 2018
Three (basic) alternatives--
  1. concatenate the shorter vectors to the length of the longest with NaN or other missing value that can be used to identify what is/isn't real data, or
  2. use a cell array which can hold each of the three disparately-sized vectors in its three cells.
  3. write all arrays from 1:minLength as three columns, then the two of next longest and finally the third last elements. Will need a placeholder in the file to keep them aligned by column appropriately.
  6 Comments
Isai Fernandez
Isai Fernandez on 3 May 2018
Edited: Isai Fernandez on 3 May 2018
I need matrices, written in this form: Matrix A:
2 2
1 5
0 5
2 1
3 0
7 8
3 1
9 2
Matrix B:
2 2
1 5
0 5
2 1
Matrix C:
2 2
1 5
0 5
2 1
3 0
7 8
3 1
9 2
2 2
1 5
0 5
2 1
3 0
2 2
1 5
0 5
2 1
3 0
7 8
3 1
9 2
and so on, all this matrices in one file, [A B C]:
2 2 2 2 2 2
1 5 1 5 1 5
0 5 0 5 0 5
2 1 2 1 2 1
3 0 3 0
7 8 7 8
3 1 3 1
9 2 9 2
2 2
1 5
0 5
2 1
3 0
2 2
1 5
0 5
2 1
3 0
7 8
3 1
9 2
Is it possible?
dpb
dpb on 4 May 2018
Edited: dpb on 4 May 2018
Yeah, but it's going to take some machinations; the problem is that any given array apparently can be the shortest and so you've got to write a dynamic format string depending on the positions in the record the data must be...
Here's an example of what you've got to programmatically generate for the example of two records--
>> fprintf([repmat('%4d',1,6) '\n'],1:6), fprintf([repmat('%4d',1,2) '%12d%4d\n'],1:2, 5:6)
1 2 3 4 5 6
1 2 5 6
>>
Here the first record is complete so the format string is the total number of nonzero elements to be written; the second record counts the first N (2) consecutive elements and then adds the number of missing elements (also 2 here, wouldn't have to be) times the field width to the field width of the first of the next M consecutive nonzero elements and follows with one less than M fields.
It's doable, but needs more time than I've got at the moment to write actual code.
ADDENDUM If it is so as the example that the first R rows
R=min(size(A,1),size(B,1),size(C,1));
are full, then that part is pretty easy...
W=6; % field width
R=min([size(A,1) size(B,1) size(C,1)]); % shortest array
fmt=[repmat(num2str(W,'%%%dd'),1,6) '\n']]; % format string 3x2 elements
fprintf(fid,fmt,[A(1:R,:) B(1:R,:) C(1:R,:)].'); % write that portion
Now comes the tricky part; who of the three is the shortest to write the remaining two in their correct places and then who's the longest to clean up with in the end.
While the above is written in terms of your specific A,B,C variables, to write generic code the only rational thing to do will be to create a cell array so can reference the proper one(s) dynamically by indexing.
And, of course, is it always only three variables and always only two values per record each? Then you've got to deal with that extra complexity if not--if so, helps in that the by twos count if fixed....again all doable; but will take some thinking through to first ensure have completely defined the requirements and then to implement a solution.
ADDENDUM 2 Implementation might be easier if you were to augment the shorter arrays with NaN and concatenate all together into one and write that whole array to memory; then do character substitution to replace the NaN markers with blank strings and subsequently write that array to file. On thinking about it, think that would likely be the simplest thing to do.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!