How to concatenate two matrices in a loop?

64 views (last 30 days)
I' m running a for loop say 'n' times,everytime a 5x2 matrix will be generted ,i have to join all these matrices such that at last run i have to get a matrix of size 5x2n .How to solve this?

Accepted Answer

James Tursa
James Tursa on 2 Mar 2015
Edited: James Tursa on 2 Mar 2015
Another way using 3-dimensional syntax:
n = number of individual matrices you have;
R = number of rows per matrix;
C = number of columns per matrix;
z = zeros(R,C,n); % Pre-allocate n pages of R x C matrices
for k=1:n
m = individual R x C matrix;
z(:,:,k) = m; % Assign to the k-th page
end
z = reshape(z,R,C*n); % Reshape into R x (C*n) matrix
  2 Comments
Oce@n
Oce@n on 28 Mar 2018
This works for double arrays. For cell arrays you should try something else (see below)
Image Analyst
Image Analyst on 29 Mar 2018
You just need to make 3 changes for cells:
  1. Use cell() instead of zeros()
  2. Use z{k} instead of z(:,:,k), and
  3. Get rid of the reshape().
Try this:
n = 3; % number of individual matrices you have;
R = 4; % number of rows per matrix;
C = 5; % number of columns per matrix;
z = cell(n, 1); % Pre-allocate n cells of R x C matrices
for k = 1 : n
m = rand(R, C); % individual R x C matrix;
z{k} = m; % Put into the k-th cell
end
celldisp(z) % Display z in the command window.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 2 Mar 2015
Maybe not the most efficient way, but for small numbers it won't make any difference. Try this - I think it's what you asked for, where the number of columns might vary.
n = 6; % 6 iterations
m = []; % Initialize to null.
for k = 1 : n
% Get the number of columns for this iteration
% and make up a sample matrix with that many columns.
numberOfColumns = randi([5,12], 1); % Random number between 2 and 12
% Get a matrix with random numbers that is
% 5 rows tall by numberOfColumns columns wide.
thisMatrix = randi(9, 5, numberOfColumns);
% Append onto m
m = [m, thisMatrix];
end
% Print final m to the command window.
m

Brendan Hamm
Brendan Hamm on 2 Mar 2015
n = 10; % Change n as necessary
z = zeros(5,2*n); % pre-allocate space
for ii = 1:2:2*n-1
x = rand(5,2); % Just to make sore data
z(:,[ii,ii+1]) = x; % Assign to the appropriate location
end
  2 Comments
Betha Shirisha
Betha Shirisha on 2 Mar 2015
@brendan thanks...
Can you make it more generalised because i have just given an example of 5x2,actual matrix is of different size with 12 coloumns,so i have to extend the expression from ii,ii+1,ii+2...
And also i'm applying matrix concatenate in a loop,where should i place the matrix x,outside ii loop (i.e current running loop) or inside ii loop?
Brendan Hamm
Brendan Hamm on 3 Mar 2015
To generalize this example:
m = 12;
n = 10; % Change n as necessary
z = zeros(5,2*m); % pre-allocate space
for ii = 1:m:m*n-1
x = rand(5,m); % Just to make sore data
z(:,ii:ii+m-1) = x; % Assign to the appropriate location
end
If your matrix x is independent of a loop, then you don't need a loop to concatenate it. In your question this is what you ask for.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!