How to create impulse from timestamps?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
dpb
el 9 de Mzo. de 2014
Editada: dpb
el 10 de Mzo. de 2014
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 comentarios
Mischa Kim
el 9 de Mzo. de 2014
Editada: Mischa Kim
el 9 de Mzo. de 2014
You could also do
x = [1 11 21 31 41 51 61 71 81 91];
n = 11;
a = x==n;
2 comentarios
Ver también
Categorías
Más información sobre Encryption / Cryptography en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!