How to concatenate two matrices in a loop?

20 次查看(过去 30 天)
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?

采纳的回答

James Tursa
James Tursa 2015-3-2
编辑:James Tursa 2015-3-2
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 个评论
Oce@n
Oce@n 2018-3-28
This works for double arrays. For cell arrays you should try something else (see below)
Image Analyst
Image Analyst 2018-3-29
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.

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2015-3-2
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 2015-3-2
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 个评论
Betha Shirisha
Betha Shirisha 2015-3-2
@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 2015-3-3
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by