English 中文(简体)
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 =)

最佳回答

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

cc = aa + [bb zeros(1,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.

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;




相关问题
java.util.Vector - alternatives

Previously I would always have thought a Vector was good to use for non-descript objects when length was unknown. As far as I was aware I thought it was thread-safe too What would change that Vector ...

Questions about vector, union, and pointers in C++

The questions I have are NOT homework questions, but I am considering using these concepts in my assignment. The context, if it helps, is like this: I need to keep track of several union instances and ...

c++ problem with polymorphism and vectors of pointers

Consider the following example code: class Foo { }; class Bar : public Foo { }; class FooCollection { protected: vector<shared_ptr<Foo> > d_foos; }; class BarCollection : public ...

matrix and vector template classes in c++

#include <array> template <typename T> class Vector4<T> { std::array<T, 4> _a; // or T _a[4]; ? }; template <typename T> class Matrix4<T> { std::array<...

sort vector by more than 1 field

How do I sort the below code by name, age and score... all three fields #include <string> #include <vector> #include <algorithm> struct student_t { std::string name; ...

Best way to store and retrieve this..?

I ve been trying all night, and talk of maps, arrays, vectors and hash_maps have filled my head. im just confused now. i posted a previous question here: C++ map really slow? problem was fixed but it ...

热门标签