difference between 2 values in a vector

6 ビュー (過去 30 日間)
A-Rod
A-Rod 2024 年 6 月 10 日
コメント済み: Star Strider 2024 年 6 月 10 日
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.

採用された回答

Star Strider
Star Strider 2024 年 6 月 10 日
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 ]
x = 1x20
0.2000 0 0 0 0.2500 0 0 0 0.3000 0 0 0.4000 0 0 0 0.1000 0 0 0 0.3500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Lv = x ~= 0
Lv = 1x20 logical array
1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1
idx = find(Lv);
dx = diff(x(Lv))
dx = 1x5
0.0500 0.0500 0.1000 -0.3000 0.2500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b = zeros(size(x));
b(idx(2:end)) = dx
b = 1x20
0 0 0 0 0.0500 0 0 0 0.0500 0 0 0.1000 0 0 0 -0.3000 0 0 0 0.2500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b(1:10)
ans = 1x10
0 0 0 0 0.0500 0 0 0 0.0500 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b(11:20)
ans = 1x10
0 0.1000 0 0 0 -0.3000 0 0 0 0.2500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.
  2 件のコメント
A-Rod
A-Rod 2024 年 6 月 10 日
it worked, thanks a lot for taking time to response and for sharing solution
Star Strider
Star Strider 2024 年 6 月 10 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2024 年 6 月 10 日
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)
Columns 1 through 18 0.2000 0 0 0 0.2500 0 0 0 0.3000 0 0 0.4000 0 0 0 0.1000 0 0 Columns 19 through 20 0 0.3500
b = zeros(size(x));
idx = find(x ~= 0);
b(idx(2:end)) = diff(x(idx));
disp(b)
Columns 1 through 18 0 0 0 0 0.0500 0 0 0 0.0500 0 0 0.1000 0 0 0 -0.3000 0 0 Columns 19 through 20 0 0.2500
  2 件のコメント
A-Rod
A-Rod 2024 年 6 月 10 日
another solution, thank you for sharing it helps a lot to understand more how matlab works
Voss
Voss 2024 年 6 月 10 日
You're welcome!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWireless Communications についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by