Question

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 =)

Was it helpful?

Solution

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

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

OTHER TIPS

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.

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))]

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top