Why am I not getting an array of values?

조회 수: 13 (최근 30일)
Justin Goh
Justin Goh 2022년 3월 9일
댓글: Justin Goh 2022년 3월 10일
I am trying to plot a sinc function and eventually use it for a convolution. The sinc function is defined as h = sin(n*pi/2)/(n*pi/2), where n = -7:7.
When I run my code as is, ith becomes a single value instead of an array. However I noticed that if I define my function as h = sin(n*pi/2), h becomes an array of 1x15, which is what I want. Can somebody explain why this happens and how to go about this.
Thank you in advance.
n = -7:7; % an array of 1x15
p = (pi*n/2);
h = sin(pi*n/2)/(pi*n/2); % this becomes a single value
h1 = sin(p)/p; % this becomes an array of 1x15

채택된 답변

Voss
Voss 2022년 3월 9일
Use element-wise division, ./
n = -7:7; % an array of 1x15
p = (pi*n/2);
h = sin(pi*n/2)./(pi*n/2) % this becomes an array of 1x15
h = 1×15
-0.0909 0.0000 0.1273 -0.0000 -0.2122 0.0000 0.6366 NaN 0.6366 0.0000 -0.2122 -0.0000 0.1273 0.0000 -0.0909
h1 = sin(p)./p % this becomes an array of 1x15
h1 = 1×15
-0.0909 0.0000 0.1273 -0.0000 -0.2122 0.0000 0.6366 NaN 0.6366 0.0000 -0.2122 -0.0000 0.1273 0.0000 -0.0909
isequaln(h,h1)
ans = logical
1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by