MATLAB Answers

How to combine matrices

3 views (last 30 days)
mmy
mmy on 10 Mar 2020
Edited: mmy on 11 Mar 2020
I have some data with txt files. Each txt file include 3965x5 doubles. So i create 3 matrices. Matrices a1, a2 and a3 (...x5). Then i use below code to convert a1, a2 and a3 to b1, b2 and b3 (...x1 column matrices). And after that i combine all b matrices to C matrix (...x3)
My question is : How can i do this with a loop? I need to do this with a loop because, i will work this with tens of "a matrices" and it takes lots of time. Could you help?

  2 Comments

Adam Danz
Adam Danz on 11 Mar 2020
@mhy, in response to your flag-comment, questions cannot be deleted after they have received comments or answers. Fortunately your quesiton doesn't contain any data at all. It merely describes the size of your variables and the transformation you'd like to do with them.
Rik
Rik on 11 Mar 2020
And just in case you decide to edit your question text away, I'll make a copy here:
I have some data with txt files. Each txt file include 3965x5 doubles. So i create 3 matrices. Matrices a1, a2 and a3 (3965x5). Then i use below code to convert a1, a2 and a3 to b1, b2 and b3 (19825x1 column matrices). And after that i combine all b matrices to C matrix (19825x3)
My question is : How can i do this with a loop? I need to do this with a loop because, i will work this with tens of "a matrices" and it takes lots of time. Could you help?
b1=[a1(:,1);a1(:,2);a1(:,3);a1(:,4);a1(:,5)]
b2=[a2(:,1);a2(:,2);a2(:,3);a2(:,4);a2(:,5)]
b3=[a3(:,1);a3(:,2);a3(:,3);a3(:,4);a3(:,5)]
C=[b1,b2,b3]
And of the flag text:
Could you delete my question please for once. I am sorry but my partner doesnt allow me to ask question about our data and code. i am sorry for this.

Sign in to comment.

Answers (3)

James Tursa
James Tursa on 10 Mar 2020

  0 Comments

Sign in to comment.


David Hill
David Hill on 10 Mar 2020
You just need to load the txt files and execute:
C=[a1(:),a2(:),a3(:)];
Do you need help loading the txt files? You could also place all the txt files into a single matrix before processing.

  0 Comments

Sign in to comment.


Adam Danz
Adam Danz on 10 Mar 2020
Edited: Adam Danz on 10 Mar 2020
Here's a full demo created prior to seeing the other answers here, but uses the same approach (which suggests you the advice given by the contributors is solid).
Create 3 text files that match the input description
a1 = (1:5).*ones(3965,1); % req. >=r2016b
a2 = (6:10).*ones(3965,1); % req. >=r2016b
a3 = (11:15).*ones(3965,1); % req. >=r2016b
writematrix(a1, 'a1.txt') % make sure this doesn't overwrite any files!
writematrix(a2, 'a2.txt')
writematrix(a3, 'a3.txt')
Read in and combine the data
% List all files
% It's better to use full paths; see fullfile()
files = {'a1.txt', 'a2.txt', 'a3.txt'};
% Read in the data and store in cell array
data = cell(1, numel(files));
for i = 1:numel(files)
data{i} = readmatrix(files{i});
end
% Combine the data & confirm that it can be combined in a matrix
b = cellfun(@(x){x(:)},data);
assert(numel(unique(cellfun(@numel, b))) == 1, 'Conversion to matrix is not possible due to unequal number of rows.')
c = cell2mat(b);
The loop-version (not recommended)
c = nan(numel(data{1}), numel(data));
for i = 1:numel(data)
c(:,i) = data{i}(:);
end
To delete the demo files
delete('a1.txt')
delete('a2.txt')
delete('a3.txt')

  2 Comments

Adam Danz
Adam Danz on 10 Mar 2020
See the updated section in my answer: The loop-version (not recommended)
Adam Danz
Adam Danz on 11 Mar 2020
Take a few minutes to look at the big picture in my answer and to understand what the code is doing. The comments will be helpful.
In your question you mentioned that the data are come from text files. The first section of my answer creates 3 matrices (a1, a2, a3) and writes them to 3 text files using writematrix(). You can skip that part since your data are already in text files. You can compare the text files I generate in my answer to your text files to make sure they look them same.
In the 2nd section of my answer I list the files that are to be read-in. Here is where you list your text files.
files = {'a1.txt', 'a2.txt', 'a3.txt'};
The rest of the code in my answer shouldn't need to be changed.

Sign in to comment.

Sign in to answer this question.

Tags