Error when ploting discrete time function

4 ビュー (過去 30 日間)
Brandon Stark
Brandon Stark 2021 年 8 月 7 日
コメント済み: Star Strider 2021 年 8 月 9 日
I defined an anonymous function as below
xn = @(n)[(n==-3),(n==-2),(n==-1),(n==0),(n==1),(n==2),(n==3)]*[3;2;1;0;1;2;3]
This functions means to describe a discrete signal
x[n]=[3 2 1 0 1 2 3]
^
However, when I attempted to plot a discrete figure for this signal using stem function:
n=[-3:3]
stem(n,xn(n))
Matlab gives the error message as following:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches
the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in @(n)[(n==-3),(n==-2),(n==-1),(n==0),(n==1),(n==2),(n==3)]*[3;2;1;0;1;2;3]
I really don't know where goes wrong.
A big thanks to all that may drop by and leave your answer!

採用された回答

Star Strider
Star Strider 2021 年 8 月 7 日
Use element-wise multiplication (.*) instead of matrix multiplication (*).
Try this —
xn = @(n)[(n==-3),(n==-2),(n==-1),(n==0),(n==1),(n==2),(n==3)].*[3;2;1;0;1;2;3];
figure
n=[-3:3]
n = 1×7
-3 -2 -1 0 1 2 3
stem(n,xn(n))
.
  8 件のコメント
Brandon Stark
Brandon Stark 2021 年 8 月 9 日
Really appreciate your answer!
I think this solves my problem perfectly.
Thx very much
Star Strider
Star Strider 2021 年 8 月 9 日
As always, my pleasure!
.

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

その他の回答 (1 件)

Paul
Paul 2021 年 8 月 7 日
編集済み: Paul 2021 年 8 月 8 日
The function definition is implicitly assuming that the input, n, is a scalar. Looking at the first the term on its LHS:
n = 2;
[(n==-3),(n==-2),(n==-1),(n==0),(n==1),(n==2),(n==3)]
ans = 1×7 logical array
0 0 0 0 0 1 0
That returns what you expect. But with n a vector
n = 1:2;
[(n==-3),(n==-2),(n==-1),(n==0),(n==1),(n==2),(n==3)]
ans = 1×14 logical array
0 0 0 0 0 0 0 0 1 0 0 1 0 0
Each logical comparison is the same size as n, then each of those are concatenated together. Clearly this result can't be element-multiplied with -3:0:3.
Edit: I just realized that the function in the quesion was relying on matrix mulitplication, not element mulitplication. But the point still stands that terms being multiplied are incompatible.
Fortunately, this example can be expressed in closed form
x = @(n)(abs(n).*(abs(n)<=3)); % assumes x(n) is zero for abs(n) > 3
x(-3:3)
ans = 1×7
3 2 1 0 1 2 3
x([-1 2])
ans = 1×2
1 2
If you have a finite duration sequence that can't be expressed in closed form, you can always just use indexing, accounting for Matlab's 1-based indexing.
x = abs(-3:3); n = -3:3;
x([-1 2]+(1-n(1)))
ans = 1×2
1 2
You can wrap this up in a function if desired, where n defines the sequence and k defines the elements of the sequence you want to return
xn = @(k,x,n)(x(k+(1-n(1))));
xn([-1 2],x,n)
ans = 1×2
1 2
xn(-1:1,x,n)
ans = 1×3
1 0 1
  7 件のコメント
Paul
Paul 2021 年 8 月 9 日
編集済み: Paul 2021 年 8 月 9 日
You can also wrap up the sequence in a structure to simplify things a bit:
x.vals = [1 0 0 3 4 0 0 6];
x.indices = -3:4;
xn = @(n,sequence)(sequence.vals(n+(1-sequence.indices(1))));
xn([-3 0 1],x)
ans = 1×3
1 3 4
Brandon Stark
Brandon Stark 2021 年 8 月 9 日
ok

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

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by