I have a question about multiplying two series...

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 件のコメント

Matt Fig
Matt Fig 2011 年 5 月 21 日
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
Matt Fig 2011 年 5 月 21 日
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?

サインインしてコメントする。

 採用された回答

Oleg Komarov
Oleg Komarov 2011 年 5 月 21 日

1 投票

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.

3 件のコメント

tri2061990
tri2061990 2011 年 5 月 21 日
My teacher will ask me about his code so can you explain meaning of y1(find((n>=min(n1))&(n<=max(n1))==1)) = x1?
Walter Roberson
Walter Roberson 2011 年 5 月 21 日
Oleg does not have such a line of code in his response.
Oleg Komarov
Oleg Komarov 2011 年 5 月 21 日
Your original line says:
* is n >= than the min of n1
* AND is (n <= than the max of n1) equal to 1 - nonsense
* then FIND the position of the combined logical operations (not needed)
Finally y(positions) = x1; assign x1 to y in positions just found above.

サインインしてコメントする。

その他の回答 (1 件)

Matt Fig
Matt Fig 2011 年 5 月 21 日

0 投票

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 件のコメント

tri2061990
tri2061990 2011 年 5 月 21 日
I have to multiply two series x1 and x2
Matt Fig
Matt Fig 2011 年 5 月 21 日
So I changed the code to multiply instead of add. Why did you change the code you originally posted?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTime Series Events についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by