How to create impulse from timestamps?

1 次查看(过去 30 天)
Fam
Fam 2014-3-9
编辑: dpb 2014-3-10
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

回答(2 个)

dpb
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.

Mischa Kim
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 个评论
Fam
Fam 2014-3-9
the x was just a sample so the period just happened to be 11. i was hoping to do it in all cases
Mischa Kim
Mischa Kim 2014-3-9
So what would be your most general case, and the expected result?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Encryption / Cryptography 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by