Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add vectors with different length?

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

like image 606
Louise Avatar asked Dec 21 '09 11:12

Louise


People also ask

What happens if you add 2 vectors of different lengths in R?

One thing to keep in mind while adding (or other arithmetic operations) two vectors together is the recycling rule. If the two vectors are of equal length then there is no issue. But if the lengths are different, the shorter one is recycled (repeated) until its length is equal to that of the longer one.

Can you add 2 vectors with different units?

To put it simply, you can only add quantities with the same units, example, weight to weight (or mass to mass), time to time, money to money, etc. It is the same thing with vectors. Acceleration and velocity are both vectors, but with different units, so should not be added.

How do you add vectors with different directions?

Starting from where the head of the first vector ends, draw the second vector to scale in the indicated direction. Label the magnitude and direction of this vector on the diagram. Draw the resultant from the tail of the first vector to the head of the last vector. Label this vector as Resultant or simply R.


3 Answers

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.

like image 126
gnovice Avatar answered Sep 22 '22 23:09

gnovice


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

cc = aa + [bb zeros(1,2)];
like image 23
Peter Avatar answered Sep 22 '22 23:09

Peter


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))]
like image 24
Andreas Brinck Avatar answered Sep 22 '22 23:09

Andreas Brinck