Vectorizing for loop for faster processing

2 ビュー (過去 30 日間)
Ifeoluwa Ajiboye
Ifeoluwa Ajiboye 2022 年 3 月 10 日
編集済み: AndresVar 2022 年 3 月 10 日
I have a column of sorted row numbers labelled "Row Numbers"
Row Numbers =
1
3
5
8
100
...
...
I want to use information from Row numbers to sort a table labbeled "Table" into different sections
Table =
Time SensorOut
1:00 1
2:00 3
3:00 2.3
4:00 3.4
5:00 3.1
6:00 30
7:00 3
8:00 3
9:00 3
... ...
... ...
... ...
What i want to achieve is this
Table =
Time SensorOut Section
1:00 1 1
2:00 3 1
3:00 2.3 2
4:00 3.4 2
5:00 3.1 3
6:00 30 3
7:00 3 3
8:00 3 3
9:00 3 4
... ... ...
... ... ...
... ... ...
What i have done so far
for i = 1:length(rownumbers)-1
Table.Section(rownumbers(i):rownumbers(i+1)) = i;
end
This works but the issue is that i have 100,000 rows of data and each loop is taking about 0.4 seconds. I was wondering if any one had an idea how to vectorize this for loop for faster processing? Or a more clever way of achiveing this?

採用された回答

AndresVar
AndresVar 2022 年 3 月 10 日
編集済み: AndresVar 2022 年 3 月 10 日
You defined bin edges so you can try discretize: Group data into bins or categories - MATLAB discretize (mathworks.com)
idx=1:10
idx = 1×10
1 2 3 4 5 6 7 8 9 10
rowNums=[1 3 5 8 inf]
rowNums = 1×5
1 3 5 8 Inf
discretize(idx,rowNums)'
ans = 10×1
1 1 2 2 3 3 3 4 4 4

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by