I have a question about multiplying two series...
Afficher commentaires plus anciens
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)?
2 commentaires
Matt Fig
le 21 Mai 2011
Originally, the second line of your code read:
Thuc hien y(n) = x1(n)+x2(n)
But now it reads:
Thuc hien y(n) = x1(n0*x2(n)
So which is it? If your teacher gave it to you, why are you changing it around in the post?
Matt Fig
le 21 Mai 2011
Also, your last line read this:
y = y1+y2;
and now reads like this:
y = y1.*y2;
If your teacher really gave you this code, what are you doing changing it around like that?
Réponse acceptée
Plus de réponses (1)
Matt Fig
le 21 Mai 2011
That is some bizarre code indeed. Would you mind translating the help text into English please?
If n1 and n2 were meant to be scalars, then I would question whether the person who wrote that code understood anything about the MIN and MAX functions. It looks like there is an attempt at indexing into y1 with the values of n that are greater than or equal to the minimum value in n1, and less than or equal to the maximum value in n1, then set them equal to x1. But I am certain this will fail except for certain conditions.
What does your teacher want you to do with the code?
If I had to guess, I would say the code is supposed to do something like this:
function y = sigadd(x1,n1,x2,n2)
% x1 a 1-by-n1 row vector, x2 a 1-by-n2 row vector.
if n1>n2
y = x1;
y(1:n2) = y(1:n2) .* x2;
else
y = x2;
y(1:n1) = y(1:n1) .* x1;
end
Such that when passed to vectors and their lengths, the vectors are added as far as they can be added. Now from the command line:
>> sigadd([4 5 6 7 8 9],6,[1 2 3],3)
ans =
4 10 18 7 8 9
2 commentaires
tri2061990
le 21 Mai 2011
Matt Fig
le 21 Mai 2011
So I changed the code to multiply instead of add. Why did you change the code you originally posted?
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!