MATLAB Answers

Find Consecutive numbers and print value in next collumn

1 ビュー (過去 30 日間)
Patrick Lonergan
Patrick Lonergan 2021 年 7 月 13 日
コメント済み: Patrick Lonergan 2021 年 7 月 14 日
Hi,
I have a table of 4 collumns with over a million rows. In collumn 4 I have a collumn that iis based on logic (when the temperature in collumn 3 exceeds a given value the entry in collumn 4 will be one and when it does not exceed it will be 0).
In collumn 5 I would like to have a count of the number of consectutive values placed in the row of the final 1.
I have used the following to get a vector of containing the consecutive counts, but I am unsure how to get them to print in collumn five at the end of each cosecutive set
f = find(diff([0,exceedlogic,0]==1));
p = f(1:2:end-1); % Start indices
y = f(2:2:end)-p; % Consecutive ones’ counts
  2 件のコメント
Patrick Lonergan
Patrick Lonergan 2021 年 7 月 13 日
The data set is rather large but I have taken some screen shots of the table. The table is in fact 5 collumns. \
As you can see collumn 5 is filled with 0 and 1 (logic), in collumn six I would like the consecutive count. For example in row 19 collumn 6 the value 1 would be inserted, while in row 247 collumn 6 should be the value of 2.
I hope that clears things up.

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

採用された回答

Image Analyst
Image Analyst 2021 年 7 月 13 日
Try this:
fprintf('Beginning to run %s.m ...\n', mfilename);
% Create sample data because none was attached to the question.
data = rand(200, 6);
data(:, 5) = data(:, 4) > 0.3;
data(:, 6) = 0
% Now we have our data and can begin.
%==================================================================
% Measure lengths of each run of 1s.
props = regionprops(logical(data(:, 5)), 'Area', 'PixelList');
% Assign area to the very last row of 1s.
for k = 1 : length(props)
lastRow = props(k).PixelList(end, 2)
data(lastRow, 6) = props(k).Area;
end
%==================================================================
fprintf('Done running %s.m.\n', mfilename);
Main code is between the ============== of course.
  1 件のコメント
Patrick Lonergan
Patrick Lonergan 2021 年 7 月 14 日
Thanks worked great, cheers

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

その他の回答 (1 件)

David Hill
David Hill 2021 年 7 月 13 日
Simple loop works
c=0;
f(:,6)=0;
for k=1:size(f,1)
if f(k,5)==1
c=c+1;
f(k,6)=c;
else
c=0;
end
end
  1 件のコメント
Patrick Lonergan
Patrick Lonergan 2021 年 7 月 14 日
Thanks

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by