Borrar filtros
Borrar filtros

How to concatenate two matrices in a loop?

18 visualizaciones (últimos 30 días)
Betha Shirisha
Betha Shirisha el 2 de Mzo. de 2015
Comentada: Image Analyst el 29 de Mzo. de 2018
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?

Respuesta aceptada

James Tursa
James Tursa el 2 de Mzo. de 2015
Editada: James Tursa el 2 de Mzo. de 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 comentarios
Oce@n
Oce@n el 28 de Mzo. de 2018
This works for double arrays. For cell arrays you should try something else (see below)
Image Analyst
Image Analyst el 29 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 2 de Mzo. de 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
  2 comentarios
Betha Shirisha
Betha Shirisha el 2 de Mzo. de 2015
@ image analyst thanks..
Oce@n
Oce@n el 28 de Mzo. de 2018
This works on cell arrays! Thank you!

Iniciar sesión para comentar.


Brendan Hamm
Brendan Hamm el 2 de Mzo. de 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 comentarios
Betha Shirisha
Betha Shirisha el 2 de Mzo. de 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 el 3 de Mzo. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by