How to concatenate two matrices in a loop?

26 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by