Unique function with rows and 'first' as arguments

Hello guys!
I have a 90002x6 matrix called "timeVec" with the following format:
2023 2 6 0 0 0
2023 2 6 0 0 0.01
2023 2 6 0 0 0.02
...
...
2023 2 6 0 15 0.01
I am trying to understand the following command:
[~,tInd,~] = unique(timVec(:,4:5),'rows','first')
The resulting tIndi s a 16x1 matrix with the following values:
1
6001
12001
18001
24001
...
...
90001
Thanks!

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 2 月 18 日

1 投票

The code is locating the first row in which each combination of hour and minute occurs.
There are other implementations, such as looking at whether diff(timVec(:,4)*60+timVec(:,5)) is non-zero . Unless, that is, the entries are out of order -- if they are potentially out of order you should probably also be using the 'stable' option of unique()

3 件のコメント

Akis Drosinos
Akis Drosinos 2023 年 2 月 18 日
thanks for the reply, much appreciated. Do you understand where the 6001, 12001, etc. come from?
Steven Lord
Steven Lord 2023 年 2 月 18 日
Row 1 is the first row of timeVec whose 4th and 5th elements is not the same as any previous row.
Row 6001 is the second row of timeVec whose 4th and 5th elements is not the same as any previous row. If I had to guess based on your pattern I'd say it contains [2023 2 6 0 1 0].
Row 12001 of timeVec is likely [2023 2 6 0 2 0].
Akis Drosinos
Akis Drosinos 2023 年 2 月 18 日
編集済み: Akis Drosinos 2023 年 2 月 18 日
spot on!
Each timestamp is 10 ms, so we need 6000 rows for one second. Column 5 shows the minutes and, as you very well said, row 1 shows the first unique input, so row 1 + 6000 will show the next and then 6001 + 6000 the next and so on!
thanks, it all makes sense now!

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2015b

質問済み:

2023 年 2 月 17 日

編集済み:

2023 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by