Getting distance between specific numbers in array after using diff() function

4 ビュー (過去 30 日間)
M
M 2022 年 6 月 12 日
コメント済み: Image Analyst 2022 年 6 月 14 日
I would like to find a distance between specific arrays (this distance represents time), I got it as an output of using diff function thanks to it I know when foot movement started. For example let's say I have array like this:
x = [ 0 1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 -1]
And I would like to automate the code and find the distance - how many zeros are between 1 and -1 (i don't want from -1 and 1), So for example from this code I'd know that I have 1) 10 zeros space between first 1 and -1 and index range between first and last zero, and for the 2) 3 zeros space and also indexes. Is that possible to do?

採用された回答

Jan
Jan 2022 年 6 月 12 日
編集済み: Jan 2022 年 6 月 12 日
x = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, -1];
pos = find(x == 1);
neg = find(x == -1);
len = neg - pos - 1
len = 1×2
10 3
If the trailing -1 is missing:
len = neg(1:numel(pos)) - pos - 1;
  5 件のコメント
Jan
Jan 2022 年 6 月 14 日
Thanks. So starting is at pos+1 and ending at neg-1.
Image Analyst
Image Analyst 2022 年 6 月 14 日
x = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, -1];
pos = find(x == 1);
neg = find(x == -1);
len = neg - pos - 1
len = 1×2
10 3
% Find out where each run of 0's start and end>
startingPositions = pos + 1
startingPositions = 1×2
3 18
endingPositions = neg - 1
endingPositions = 1×2
12 20
It seems to miss the zero starting and ending at index 1, and the run starting at 14 and ending at 16.
Maybe search for the zeros directly instead of for 1 and -1.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 12 日
編集済み: Image Analyst 2022 年 6 月 13 日
If you have the Image Processing Toolbox you can use regionprops to find the location of the "0" regions:
x = [ 0 1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 -1];
props = regionprops(x == 0, 'Area', 'PixelIdxList');
allZeroLengths = [props.Area]
allZeroLengths = 1×4
1 10 3 3
for k = 1 : numel(props)
startingIndexes(k) = props(k).PixelIdxList(1);
endingIndexes(k) = props(k).PixelIdxList(end);
end
startingIndexes % Show in command window
startingIndexes = 1×4
1 3 14 18
endingIndexes
endingIndexes = 1×4
1 12 16 20

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by