필터 지우기
필터 지우기

How to concatenate two matrices in a loop?

조회 수: 18 (최근 30일)
Betha Shirisha
Betha Shirisha 2015년 3월 2일
댓글: Image Analyst 2018년 3월 29일
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
  댓글 수: 2
Betha Shirisha
Betha Shirisha 2015년 3월 2일
@ image analyst thanks..
Oce@n
Oce@n 2018년 3월 28일
This works on cell arrays! Thank you!

댓글을 달려면 로그인하십시오.


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