MATLAB: I have a question about multiplying two series…

multiply

My teacher give me a code
function [y,n] = sigadd(x1,n1,x2,n2)
%Thuc hien y(n) = x1(n0*x2(n)
%----------------------------------------------
%[y,n] = sigadd(x1,n1,x2,n2)
% y = day tong co vector chi so n
%x1 = day thu nhat co vector chi so n1
%x2 = day thu hai co vector chi so n2 (n2 co the khac n1)
n = min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1)) = x1;
y2(find((n>=min(n2))&(n<=max(n2))==1)) = x2;
y = y1.*y2;
I dont know meaning of y1(find((n>=min(n1))&(n<=max(n1))==1)) = x1;
If I choose x1=[1 0 0 2 0] then I have to choose n1=length(x1)?

Best Answer

You can simplify the code to:
n = min([n1(:); n2(:)]): max([n1(:),n2(:)]);
y1 = zeros(1,length(n));
y2 = y1;
y1(n >= min(n1) & n <= max(n1)) = x1;
y2(n >= min(n2) & n <= max(n2)) = x2;
y = y1.*y2;
The find part and == 1 was really unnecessary.
Now basically you index in y1 the values which are in the range [min(n1) max(n1)] and asign to those values x1.
Related Question