difference between 2 values in a vector
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello Community.
Asking for your help again to find a solution.....
I have a vector with several data points, I'd like to get the difference between each value that is not equal to CERO.
I'm using "diff" command however this includes the difference betwee values that are equal to CERO, here is an example.
x = [0.2 0.0 0.0 0.0 0.25 0.0 0.0 0.0 0.3 0.0 0.0 0.4 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.35 ]
I tried this : b = diff(x)
b = -0.20 0.0 0.0 0.25 -0.25 0.0 0.0 0.30 -0.30 0.0 0.40 -0.40 0.0 0.0 0.10 -0.10 0.0 0.0 0.35
it gives me the difference between each data point
What I'm trying to get is the difference between each point that is not equal to cero like this:
b = 0.0 0.0 0.0 0.0 0.05 0.0 0.0 0.0 0.05 0.0 0.0 0.1 0.0 0.0 0.0 -0.3 0.0 0.0 0.0 0.25
I don't want to eliminate the ceros in between if I do so it will change the length of the vector and I need to keep same lenght to compare Vs other signals.
the number of ceros between each data value is not always the same, could be 3 ceros in between but it could be 2, 4, 6.....
as always I thank you in advance, your feedback will be higly appreciated.
0 Commenti
Risposta accettata
Star Strider
il 10 Giu 2024
Try this —
x = [0.2 0.0 0.0 0.0 0.25 0.0 0.0 0.0 0.3 0.0 0.0 0.4 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.35 ]
Lv = x ~= 0
idx = find(Lv);
dx = diff(x(Lv))
b = zeros(size(x));
b(idx(2:end)) = dx
b(1:10)
b(11:20)
.
2 Commenti
Più risposte (1)
Voss
il 10 Giu 2024
x = [0.2 0.0 0.0 0.0 0.25 0.0 0.0 0.0 0.3 0.0 0.0 0.4 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.35 ];
disp(x)
b = zeros(size(x));
idx = find(x ~= 0);
b(idx(2:end)) = diff(x(idx));
disp(b)
Vedere anche
Categorie
Scopri di più su Wireless Communications in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!