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 Kommentar

Ashmil Mohammed
Ashmil Mohammed am 10 Jul. 2015
So you need to add zeros to the beginning of A for every extra element in B?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Guillaume
Guillaume am 10 Jul. 2015

2 Stimmen

newA = [zeros(1, max(0, numel(B)-numel(A))), A]

Weitere Antworten (2)

Image Analyst
Image Analyst am 10 Jul. 2015

1 Stimme

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by