How to add zeros to the end of an 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.

2 comentarios

Becky CNS
Becky CNS el 15 de Mzo. de 2018
Movida: Dyuman Joshi el 29 de Nov. de 2023
I am trying to do this but with A and B as matrices. The error message I get is 'Dimensions of matrices being concatenated are not consistent'. All I want to do is add zeros to another vector the length of A-B. How would I change the above code?
Jan
Jan el 15 de Mzo. de 2018
Movida: Dyuman Joshi el 29 de Nov. de 2023
If you post your code, we could fix it directly. This would be more convenient and useful than to create an artificial example.
A = rand(3,4);
B = rand(2,2);
B(size(A,1), size(A,2)) = 0;
A - B

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 29 de En. de 2013
Editada: Jan el 29 de En. de 2013
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))];

12 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 29 de En. de 2013
What if A and B have the same size? (for the first code)
Jan
Jan el 29 de En. de 2013
Editada: Jan el 29 de En. de 2013
In the OP's problem, the sizes of A and B are 10 and 7. For more general problems:
nA = numel(A);
nB = numel(B);
if nA > nB
B(nA) = 0;
elseif nB > nA
A(nB) = 0;
end
Mori
Mori el 1 de Jul. de 2015
This adds zero at the end, what if we need first zeros and then elements of B?
B = [zeros(1, length(A) - length(B)), B];
Mori
Mori el 1 de Jul. de 2015
this command works perfectly for raw array but not for column, any idea?
B=[zeros(length(A) - length(B),1);B]
John Lutz
John Lutz el 13 de Oct. de 2017
Thanks Andrei Bobrov,for
B = [zeros(1, length(A) - length(B)), B];
this worked perfectly for me. Maybe someone will see this and provide an explanation! I couldnt make sense of it from the matlab zero doc
Jan
Jan el 13 de Oct. de 2017
@John Lutz: zeros(1,n) creates a row vector of n zeros. [z,B] concatenates the arrays z and B horizontally. length(A)-length(B) determines the difference of the lengths of the two arrays.
Black4Ghost
Black4Ghost el 18 de Mzo. de 2020
I am sorry I am commenting so late on this Thread but I have the same Issue with the only difference that I don't want zeros to fill out my cell array. I want it filled out with chars of the value '0'. Also sorry for not opening a new question but its almost the same problem I am afraid.
kay Dee
kay Dee el 2 de Abr. de 2020
thx worked for me
Md Rezaul Karim
Md Rezaul Karim el 26 de En. de 2021
Thank you Jan
Jan
Jan el 26 de En. de 2021
@Black4Ghost: Filling with characters works by:
[YourData, rempmat('0', 1, wantedWidth - numel(YourData))]

Iniciar sesión para comentar.

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 29 de En. de 2013
Editada: Azzi Abdelmalek el 29 de En. de 2013
A=[ 1 5 3 8 9 4 7 6 5 2 ];
B=[ 5 2 3 9 7 4 5 ];
B(end:numel(A))=0

2 comentarios

Jan
Jan el 29 de En. de 2013
B(end) overwrites the last element of B.
B(end+1:numel(A))=0

Iniciar sesión para comentar.

If you're using release R2023b or later, you could use paddata or resize to do this.
A=[ 1 5 3 8 9 4 7 6 5 2 ];
B=[ 5 2 3 9 7 4 5 ];
n = max(length(A), length(B)); % using length is okay since A and B are both vectors
If we always want to make the vector longer or keep it the same size, use paddata.
A2 = paddata(A, n)
A2 = 1×10
1 5 3 8 9 4 7 6 5 2
B2 = paddata(B, n)
B2 = 1×10
5 2 3 9 7 4 5 0 0 0
If you want to append to the vector or remove elements depending on whether it's shorter or longer than your desired size, use resize.
A3 = resize(A, 8) % drop last 2 elements
A3 = 1×8
1 5 3 8 9 4 7 6
B3 = resize(B, 8) % add one 0 at the end
B3 = 1×8
5 2 3 9 7 4 5 0
There's also trimdata to always make it the desired length or shorter.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de En. de 2013

Respondida:

el 29 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by