- #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:
<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 !
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
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: