ADDING ZEROS to the front of array

조회 수: 64 (최근 30일)
ANDREW LIU
ANDREW LIU 2015년 7월 10일
답변: Mohammad Sohail Khan 2017년 10월 3일
May I ask for help
I have two arrays like A=[ 111000 ] & B=[ 1111000 ]
I would like to make A become [0111000],or for example if B=[101111000],A becomes[000111000].
Thank you
  댓글 수: 1
Ashmil Mohammed
Ashmil Mohammed 2015년 7월 10일
So you need to add zeros to the beginning of A for every extra element in B?

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 7월 10일
newA = [zeros(1, max(0, numel(B)-numel(A))), A]

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 7월 10일
Simply pad the left with zeros if necessary to make them the same length, then AND the vectors. Here it is bot both cases you gave:
% Case #1
A=[1,1,1,0,0,0]
B=[1,1,1,1,0,0,0]
% Make them the same size.
% If A is shorter, prepend zeros
la = length(A)
lb = length(B)
if la < lb
A = [zeros(1, lb-la), A]
end
% Do the operation for case #1:
A = A & B
% Now let's do Case #2
A=[1,1,1,0,0,0]
B=[1,0,1,1,1,1,0,0,0]
% Make them the same size.
% If A is shorter, prepend zeros
la = length(A)
lb = length(B)
if la < lb
A = [zeros(1, lb-la), A]
end
% Do the operation for case #2:
A = A & B

Mohammad Sohail Khan
Mohammad Sohail Khan 2017년 10월 3일
Thank You

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by