How to create impulse from timestamps?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
dpb
il 9 Mar 2014
Modificato: dpb
il 10 Mar 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 Commenti
Mischa Kim
il 9 Mar 2014
Modificato: Mischa Kim
il 9 Mar 2014
You could also do
x = [1 11 21 31 41 51 61 71 81 91];
n = 11;
a = x==n;
2 Commenti
Vedere anche
Categorie
Scopri di più su Encryption / Cryptography in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!