How to create impulse from timestamps?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi i have an array x=[ 1 11 21 31 41 51 61 71 81 91] i would like to create an impulse function of a=[0 1 0 0 0 0 0 0 0 0 0 1 0 0 ....] my script is as follows
function= zeros(1,max(a)
for n=1 : max(a)
if n==a
x(n)=1
end
end
but what i get is just function = [ 0 0 0 0....] seems like this line " if n==a x(n)=1 " isnt working
댓글 수: 0
답변 (2개)
dpb
2014년 3월 9일
편집: dpb
2014년 3월 10일
function= zeros(1,max(a)
...
The above is surely not your actual attempt is it?
If so, and you named it owing to Matlab convention you just aliased the builtin zeros which undoubtedly will lead to mass confusion and errors going forward.
Try pulse or somesuch name instead...
function p = pulse(idx)
% return array w/ 1 at positions in idx beginning w/ a 0
p=zeros(1,max(x)+1); % zero vector of length max pos'n + initial 0
p(idx)=1;
In Matlab, don't need any loops or tests, just use the values as the index vector.
ADDENDUM:
The above assumes a given vector location--if you want instead a fixed difference, then you'll have to define how many pulses you want to determine the length but you can then dispense with the input vector in favor of an interval and number--
function p = pulseb(dx,N)
% return array w/ 1 at N positions in idx beginning w/ a 0
% with spacing of dx
idx=1:dx:N*dx; % the index vector from 1 for N pulses
p=zeros(1,max(idx)+1); % zero vector of length max pos'n + initial 0
p(idx)=1;
ADDENDUM 2:
Or, of course, you could have more general logic and use the number of parameters passed to the function imply the form--two could be the SPACING, COUNT whereas >3 could be interpreted as the LOCATIONS.
댓글 수: 0
Mischa Kim
2014년 3월 9일
편집: Mischa Kim
2014년 3월 9일
You could also do
x = [1 11 21 31 41 51 61 71 81 91];
n = 11;
a = x==n;
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Encryption / Cryptography에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!