How to count the amount of times certain row changes in a table?

2 ビュー (過去 30 日間)
Richard Michaud Langis
Richard Michaud Langis 2022 年 5 月 3 日
編集済み: dpb 2022 年 5 月 3 日
So I have a table, 8 columns and a varying amount of rows. The last 3 columns are always going to be [ 1 0 0 ], [ 0 1 0 ] or [ 0 0 1 ]. I would like to be able to count the amount of times, one row of these 3 columns that are not the same as the previous one (ex: row1 [ 0 1 0 ], row2 [ 0 1 0 ], row 3 [ 0 0 1], this counts as 2). Would taking these 3 columns apart help, if so how?

回答 (1 件)

dpb
dpb 2022 年 5 月 3 日
" Would taking these 3 columns apart help...?"
No.
tT.Diff=[false;any(diff(tT.X),2)];
  4 件のコメント
Richard Michaud Langis
Richard Michaud Langis 2022 年 5 月 3 日
head(T)
ans =
8×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8
____ ____ ____ ____ ____ ____ ____ ____
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
So Basically, I want to count how many times the data in Var6, Var7, Var8 are different in one row from the previous one.
dpb
dpb 2022 年 5 月 3 日
編集済み: dpb 2022 年 5 月 3 日
Yes, so what's the problem? It's a little simpler in your first scenario where the columns were all in one array, but the same idea.
tDiff=[false; any(diff(tT{:,{'Var6','Var7','Var8'}}),2)];
See the link @doc:table -- specifically the subsection at bottom on accessing data from a table.
The count is simply
sum(tDiff)
If you don't need to know where the changes are, then you don't need the new table variable, it can just be a temporary without assignment.

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

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by