Creating sub arrays from a larger array

23 ビュー (過去 30 日間)
Arcot
Arcot 2022 年 9 月 12 日
編集済み: Arcot 2022 年 9 月 12 日
I have a large array of size 20000x2 which contains sensor data and of this array. The first column contains datetime and the data on the second column column contains sensor data which is of interest here. The sample time for this sensor is at 0.1sec and I now have sensor data that is uneven. In simple words, the data is recorded whenever there is a movement and the rest of the cells are zero as in the example below.
Example: [0,0,0,....,1,1.5,2,2,2.8,3,3.2,4,5,...,0,0,0,0,....4,4.5,4,5,8,8.3,8.4,9] and the pattern repeats.
I am looking to split this array into segments based on the change in data from 0 to any value to capture the valid data in the format as shown in the picture, if possible.
ToDo:
  1. Get the indexes at which the changes occurs.
  2. Capture the data into segments --> [1,1.5,2,2,2.8,3,3.2,4,5] in the first subarray, [4,4.5,4,5,8,8.3,8.4,9] in the second subarray and so on along with its corresponding value from the first column
Thank you in advance.

回答 (1 件)

John D'Errico
John D'Errico 2022 年 9 月 12 日
編集済み: John D'Errico 2022 年 9 月 12 日
DON'T split them into separate, named arrays. This is just a bad way to write code. It will create buggy, slow code, even if you do make it work.
How SHOULD you do it? Put each block into a separate cell of a cell array. Now they are all still in one place, but each segment is now in its own separate place, easily and efficiently accessed.
How to do that operation? Just use a loop. You don't need anything highly vectorized, one command that will do it for you. Simple, one time operations like this are perfect for loops.
Can you do it using vectorized code? Well, yes. In fact, that is easy enough to do, to locate the indices where a segment starts and stops. The trick is to look for the point where a zero changes into a non-zero. So if the vector is called V, you might do this:
V = [0,0,0,1,1.5,2,2,2.8,3,3.2,4,5,0,0,0,0,4,4.5,4,5,8,8.3,8.4,9];
startlocs = strfind([0,V ~= 0],[0 1])
startlocs = 1×2
4 17
endlocs = strfind([V ~= 0,0],[1,0])
endlocs = 1×2
12 24
  1 件のコメント
Arcot
Arcot 2022 年 9 月 12 日
編集済み: Arcot 2022 年 9 月 12 日
Perfect!
I will embed this piece and test it out. Thank you.
However, what is the best way to put these segments into different cell arrays? I reckon a loop should do the work, but is there a better way? If yes, how?

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

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by