Filter löschen
Filter löschen

How to create impulse from timestamps?

1 Ansicht (letzte 30 Tage)
Fam
Fam am 9 Mär. 2014
Bearbeitet: dpb am 10 Mär. 2014
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

Antworten (2)

dpb
dpb am 9 Mär. 2014
Bearbeitet: dpb am 10 Mär. 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.

Mischa Kim
Mischa Kim am 9 Mär. 2014
Bearbeitet: Mischa Kim am 9 Mär. 2014
You could also do
x = [1 11 21 31 41 51 61 71 81 91];
n = 11;
a = x==n;
  2 Kommentare
Fam
Fam am 9 Mär. 2014
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 am 9 Mär. 2014
So what would be your most general case, and the expected result?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by