MATLAB: How to add zeros to the end of an array array I have two arrays like A=[ 1 5 3 8 9 4 7 6 5 2 ] & B=[ 5 2 3 9 7 4 5 ] In 'A', I have 10 elements(say m) and in 'B', I have 7(say n) elements. I need to add 10-7=3 (m-n) zeros to the end of B. Please help. Best Answer Matlab fills missing elements with 0 automatically:A = [1 5 3 8 9 4 7 6 5 2];B = [5 2 3 9 7 4 5];B(numel(A)) = 0;Less efficient, but working also:B = [B, zeros(1, length(A) - length(B))]; Related SolutionsMATLAB: Shift array to left or right, keep length and feel zero empty area I'm not aware of a function that will do exactly what you describe. However, the final result is still possible if you are willing to break the process up into steps.A = [1 2 3 4 5 6 7 8 9];B = zeros(size(A));B(3:end) = A(1:7)B = 1×9 0 0 1 2 3 4 5 6 7 % orn = -2;B = circshift(A,n);if n>0 B(1:n) = 0else B(end+n+1:end) = 0endB = 1×9 3 4 5 6 7 8 9 0 0 ● MATLAB: ADDING ZEROS to the front of array newA = [zeros(1, max(0, numel(B)-numel(A))), A] Related QuestionHow to change part of a matrix into zeroHow to make vectors zero when adding two different size vectorsHow to add zeros column and row at the beginning of matrixHow to solve this only using loopingHow to Pad an Array with extra zeros
Best Answer