concatenate different matrices with different dimensions
Show older comments
Hello, I am obtaining 3 matrices with two colums each of then (the first column contains the time and the second column contains the acceleration) that represents the acceleration components of earthquake signals that can be represented as X, Y and Z matrices. So, these matrices may have the following size: X (520x2), Y(480x2) and Z(500x2) as an example. If I want to concatenate the matrices horizontally using this command:
The final size of the matrix (data) should be 520x6 (based on the highest length of colums of the three matrices X,Y and Z). So, zeros should be added at the end of each matrix with lower column size convert Y(480x2) and and Z(500x2) into Y(520x2) and Z(520x2) and then the three matrices be concatenated horizontally to create the matrix data(520x6). Thank you for your help.
1 Comment
Jorge Luis Paredes Estacio
on 29 Jun 2023
Accepted Answer
More Answers (1)
Sushma Swaraj
on 29 Jun 2023
Hi,
Assuming you have X,Y and Z matrices with different sizes:
% Determine the maximum no.of rows among X,Y and Z
maxRows = max([size(X, 1), size(Y, 1), size(Z, 1)]);
% Create zero matrices to match the no.of rows
Y_zeros = zeros(maxRows, size(Y, 2));
Z_zeros = zeros(maxRows, size(Z, 2));
% Assign the original data to the zero matrices
Y_zeros(1:size(Y, 1), :) = Y;
Z_zeros(1:size(Z, 1), :) = Z;
% Concatenate X,Y_zeros and Z_zeros horizontally
data = horzcat(X, Y_zeros, Z_zeros);
Hope it helps!
1 Comment
Jorge Luis Paredes Estacio
on 29 Jun 2023
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!