From: Jonathan on
I know you can't just add two different sized arrays, so i'd like to do the following:

This cant be done:
123456 123456
123456 123456
123456 + 123456
123456
123456


So add 0s to the top
123456 000000
123456 000000
123456 + 123456
123456 123456
123456 123456


I'd like to add 0 rows to the top of the array (not bottom), but im not sure as how to do it. Can anyone point me in the right direction?

Thanks
From: Jan Simon on
Dear Jonathan,

> I know you can't just add two different sized arrays, so i'd like to do the following:
>
> This cant be done:
> 123456 123456
> 123456 123456
> 123456 + 123456
> 123456
> 123456
>
>
> So add 0s to the top
> 123456 000000
> 123456 000000
> 123456 + 123456
> 123456 123456
> 123456 123456
>
>
> I'd like to add 0 rows to the top of the array (not bottom), but im not sure as how to do it. Can anyone point me in the right direction?

A = rand(5, 1);
B = rand(3, 1);
lenA = size(A, 1);
lenB = size(B, 1);
A(lenA - lenB + 1:lenA, :) = A(lenA - lenB + 1:lenA, :) + B;
Or:
A = rand(5, 1);
B = rand(3, 1);
lenA = size(A, 1);
lenB = size(B, 1);
B2 = zeros(lenA, 1);
B2(lenA - lenB + 1:lenA) = B;
A = A + B2;

Good luck, Jan
From: Steven_Lord on


"Jonathan " <preaclis(a)guerrillamailblock.com> wrote in message
news:i21cod$p8h$1(a)fred.mathworks.com...
> I know you can't just add two different sized arrays, so i'd like to do
> the following:
>
> This cant be done:
> 123456 123456
> 123456 123456
> 123456 + 123456
> 123456 123456

Assuming that the ASCII art above is trying to represent:

repmat(123456, 5, 1) + repmat(123456, 3, 1)

then you're correct.

> So add 0s to the top
> 123456 000000
> 123456 000000
> 123456 + 123456
> 123456 123456
> 123456 123456
>
>
> I'd like to add 0 rows to the top of the array (not bottom), but im not
> sure as how to do it. Can anyone point me in the right direction?

Jan has showed you an alternate approach, where you add just the piece
common to the two arrays. But if you do need to pad an array for some other
reason in the future, use the ZEROS, ONES, NAN, INF, etc. functions and
concatenate the pad and the array being padded together.

A = repmat(123456, 3, 1);
pad = zeros(2, 1);
padded = [pad; A]

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com