How to add vectors with different length?

21,387

Solution 1

This doesn't make any sense mathematically, but if you insist, you can do this:

cc = aa + [bb zeros(1,2)];

Solution 2

For the 1-D case dealing with a set of vectors, the other answers point out the correct solutions (involving padding the shorter vector with zeroes or performing the addition using a subindex into the longer vector). However, since you mentioned in a comment that you are ultimately wanting to add two grayscale images together, I thought I'd show you a more general 2-D solution for matrices.

First, I'll load some built-in MATLAB sample images and get their sizes:

image1 = rgb2gray(imread('peppers.png'));
image2 = imread('cameraman.tif');
[r1, c1] = size(image1);
[r2, c2] = size(image2);

Notice that I converted the RGB image to grayscale first using rgb2gray. Next, I'll make a new matrix of zeroes whose size is the maximum of the sizes of the two images:

newImage = zeros(max(r1, r2), max(c1, c2), 'uint8');

Notice that I included 'uint8' in the call to zeros, since you want the matrix of zeroes to be the same type as your images so that subsequent operations on them will work correctly. The matrix newImage is now large enough to contain either of the two images. Finally, the images can be added to the new image like so:

newImage(1:r1, 1:c1) = image1;                       % Insert image 1
newImage(1:r2, 1:c2) = newImage(1:r2, 1:c2)+image2;  % Add image 2

And you can view them with the following:

imagesc(newImage);
colormap(gray);

enter image description here

NOTE: One important thing to consider is the type you use for the images. Normally, image data that is loaded into MATLAB is of type uint8. However, you may notice that adding two 8-bit unsigned integer images as I did above can result in saturation where pixels exceed the value of 255 (the maximum value for an 8-bit unsigned integer). The result is that parts of the image look bright white and lose detail (notice some of the peppers that overlap the smaller image above). You may want to avoid this by scaling the values in your images before adding them, or by converting your images to type double to perform the operations and then scaling them before resaving the image.

Solution 3

I haven't used MATLAB in ten years but I think you'll have to do something like:

cc = aa + [bb  zeros(1, length(aa) - length(bb))]

Solution 4

If it is a given that aa is bigger than bb, then I would do this:

cc = aa;
cc(1:length(bb)) = cc(1:length(bb)) + bb;
Share:
21,387
Louise
Author by

Louise

Updated on July 09, 2022

Comments

  • Louise
    Louise almost 2 years

    I would like to add two vectors with different lengths in Matlab or Octave. E.g.

    aa = [1 2 3 4];
    bb = [100 100];
    

    Which should result in a vector cc containing

    cc = [101 102 3 4]
    

    Can anyone figure out how to do this?

    Update: This is the code I ended up with for the signals that I then later convert to grey scale images.

    load train;
    t = y;
    load chirp;
    c = y;
    
    tc = c + [t; zeros(length(c) - length(t),1)];
    
    plot(1:length(tc),tc)
    

    Thank you very much to you all =)

  • Louise
    Louise over 14 years
    Cool. It is because I have 2 matrixs, which actually are gray scale pictures I would like to add. So now I would like to generalize your solution to matrixs =)
  • Louise
    Louise over 14 years
    It gives "Error: Unbalanced or unexpected parenthesis or bracket." But "cc = aa + [bb zeros(1,length(bb))];" is working =)
  • Andreas Brinck
    Andreas Brinck over 14 years
    @Louise, My bad, it should have been length and not size. That zeros(1,length(bb)) works is just a coincidence since length(bb) = length(aa) - length(bb).
  • Louise
    Louise over 14 years
    WOW! Thank you so much for that very detailed solution! That was exactly what I wanted to do. =)
  • fr_andres
    fr_andres over 7 years
    yes it does, for example, when treating signals as vectors (means the z-transform)