Simple addition of vectors of different lengths

In summary, the function vecadd(y,b,c) allows for the addition of vectors of different lengths with a shift of c. Elements that do not align are assumed to be zero. The function works when the vectors are in the order y, b, and c, but not when they are in the order b, y, and c, resulting in a "Matrix dimensions must agree" error. The for loop in line 9 may be causing this issue.
  • #1
binbagsss
1,254
11
I want it to be able to add vectors of different length with a 'shift' of c, and where elements do not align to assume a zero in the place of that,

My function is:

Matlab:
%c is the parameter shift
function[sum]=vecadd(y,b,c)

p=length(input)
q=length(a)

if p > q
for i= 1+delay:p+delay
    input(1+delay:p-delay)=input+a
    break
end
    elseif q>p
    for i= 1+delay:q+delay
    a(1+delay:q-delay)=input+a(1+delay:q-delay)
    break
   
   
    end
<Moderator's note: please use code tags when posting code.>I have tested on the vectors y=[ 1 2 3 4] and b= [1 2 3 4 5 6 7 8].

vecadd(y,b,c) works whereas the order vecadd(b,y,c) does not and it comes up with the error:
"Matrix dimensions must agree. Error in vecadd (line 9) input(1+c:p-c)=input+a"

However, it should not be carying out that for loop I thought, the first for loop, since the first if statement is not true, and it should just be skipping to the next elseif statement? why is this for loop being carried out still?

Many thanks !
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Your function has arguments y, b and c, but then you start doing stuff on a and input. Where are these defined? I might just be really bad at reading Matlab code...
 
  • #3
Office_Shredder said:
Your function has arguments y, b and c, but then you start doing stuff on a and input. Where are these defined? I might just be really bad at reading Matlab code...
I don't think so. No matter which programming language one uses, the inputs to a function should be in its parameter list, not via variables defined outside of the function.

Also, how does sum ever get set?
 

1. What is simple addition of vectors of different lengths?

Simple addition of vectors of different lengths is the process of combining two or more vectors with different magnitudes and directions to find their resultant vector.

2. How do you add vectors of different lengths?

To add vectors of different lengths, you first need to break down each vector into its components, usually along the x and y axes. Then, add the corresponding components together to get the resultant vector.

3. Can vectors of different lengths be added together?

Yes, vectors of different lengths can be added together as long as they have the same dimensionality. This means that they have the same number of components, such as both being two-dimensional or three-dimensional vectors.

4. What happens if the vectors of different lengths have different directions?

If the vectors have different directions, their resultant vector will be the sum of their individual components along the respective axes. This means that the resultant vector may have a different magnitude and direction than either of the original vectors.

5. What is the purpose of adding vectors of different lengths?

The purpose of adding vectors of different lengths is to find the overall effect or displacement when multiple forces or movements are acting on an object. It allows us to determine the resultant vector, which can be used to calculate other properties, such as velocity or acceleration.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
0
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
974
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
991
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top