Subtracting elements within a table

12 ビュー (過去 30 日間)
Lu Da Silva
Lu Da Silva 2022 年 2 月 13 日
コメント済み: Lu Da Silva 2022 年 2 月 15 日
I have a table:
1 23
2 24
3 21
4 34
1 89
2 23.2
3 34.8
4 -23.1
and it continues with the first column always being in the sequence 1-2-3-4.
I need to subract the first element next to 1 from the second element next to 1, so: 89-23. Then all elemets represented by the number 2 in the 1st column, so: 23.2-24. etc.
The outcome should be the following vector: [66 -0.8 13.8 -57.1].

採用された回答

Arif Hoq
Arif Hoq 2022 年 2 月 13 日
A=[1 23
2 24
3 21
4 34
1 89
2 23.2
3 34.8
4 -23.1];
My_Table= table(A(:,1),A(:,2))
My_Table = 8×2 table
Var1 Var2 ____ _____ 1 23 2 24 3 21 4 34 1 89 2 23.2 3 34.8 4 -23.1
B=table2array(My_Table(:,2));
ind1 = 1:4;
ind2 = ind1 + 4;
output=B(ind2)-B(ind1)
output = 4×1
66.0000 -0.8000 13.8000 -57.1000
  1 件のコメント
Lu Da Silva
Lu Da Silva 2022 年 2 月 15 日
worked perfectly, thanks!
Since the table is actually longer, I made a small adjustment:
ind1 = 1:length(B)-4;

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by