loop indexing length of non zeroes

1 回表示 (過去 30 日間)
Morten Jørgensen
Morten Jørgensen 2019 年 6 月 2 日
編集済み: Adam Danz 2019 年 6 月 10 日
Hi, I'm trying to make a loop that finds the length of a sequence of numbers:
I want to find when x(:,4) is not equal to zero, and then find how long it's is the same number, I want to save the number and the start row, and the end row.
my output should look something like this:
Id start end length
10 1320 1331 11
9 1352 1354 3
Screenshot 2019-06-02 at 14.48.05.png
thanks for any help
  3 件のコメント
Morten Jørgensen
Morten Jørgensen 2019 年 6 月 3 日
thanks a lot
Is it possible to igore when there i zero?
Adam Danz
Adam Danz 2019 年 6 月 5 日
編集済み: Adam Danz 2019 年 6 月 10 日
Have you tried the answer I proposed below? It ignores 0s as you described.

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

回答 (2 件)

Adam Danz
Adam Danz 2019 年 6 月 3 日
編集済み: Adam Danz 2019 年 6 月 4 日
Here are the steps needed to build your table.
% Create fake data that looks like your example
x = rand(2000,5);
x(:,4) = 0;
x(1320:1331,4) = 10;
x(1352:1354,4) = 9;
% calculate table components
isZero = x(:,4)==0;
dz = [0;diff(isZero)];
start = find(dz == -1); %start column
endIdx = find(dz == 1)-1; %end column
ID = x(start,4); %ID column
len = endIdx - start; %Length column
% Create table
T = table(ID,start,endIdx,len,'VariableNames', {'Id','start','endIdx','length'});
Result
T =
2×4 table
Id start endIdx length
__ _____ ______ ______
10 1320 1331 11
9 1352 1354 2
* Note that 'end' is not a valid variable name nor a valid table column name.

Steven Lord
Steven Lord 2019 年 6 月 3 日
You can use the ischange function to identify abrupt changes in your data (the jump from 0 to 10 and the drop from 10 back down to 0.)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by