make matrices same size in matlab

If you have two matrices in MATLAB with different dimensions and you want to make them the same size, you can use the zeros() function to create a matrix of the desired size and then copy the values from the original matrices into the new matrices.

main.m
% Example matrices
A = [1 2 3;
     4 5 6];

B = [7 8;
     9 10];

% Create a matrix of zeros with the desired size
C = zeros(size(A));

% Copy the values of A into C
C(1:size(A,1), 1:size(A,2)) = A;

% Create a matrix of zeros with the desired size
D = zeros(size(A));

% Copy the values of B into D
D(1:size(B,1), 1:size(B,2)) = B;
335 chars
19 lines

In the above code, we create a matrix of zeros C with the same size as matrix A using the size() function. We then use indexing to copy the values of A into C. Similarly, we create a matrix of zeros D with the same size as matrix B and then copy the values of B into D. After these operations, C and D will be the same size.

gistlibby LogSnag