Finding the number of rows to the next row containing a 1

조회 수: 2 (최근 30일)
andyc100
andyc100 2020년 8월 27일
편집: Bruno Luong 2020년 8월 28일
Hi
I have a column vector of 1s and 0s and I want to find find the number of rows to the next row containing a 1. For example:
A = [0 0 0 1 0 1 1 1 0 0 0 0 0 1]';
I would like the code to return
B = [3 2 1 0 1 0 0 0 5 4 3 2 1 0]';
Is there a vectorized way that this can be done?
Thanks in advance
  댓글 수: 10
Bruno Luong
Bruno Luong 2020년 8월 28일
Very convincing Stephen. In my laptop it's even more obvious
t Rik = 0.680010 [s]
t Bruno = 0.378046 [s]
t Stephen = 0.107799 [s]
In your for-loop code I would cast B initialization in double (in case A is logical)
B = double(A);
or
B = zeros(size(A));
Bruno Luong
Bruno Luong 2020년 8월 28일
편집: Bruno Luong 2020년 8월 28일
As I'm slightly surprised by the performance of the for-loop (I would expext it's good but not THAT good), I then try to see how far it from a MEX implementation. And I'm stunned, it's almost as fast (even faster for smaller array).
t Rik = 0.651531 [s]
t Bruno = 0.379362 [s]
t Stephen = 0.104442 [s]
t MEX = 0.073168 [s]
The code (benchmark + Cmex) is in the attacheh file for those who wants to play with.
I must congratulat TMW for improving the for-loop performance over many years.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 8월 27일
편집: Rik 2020년 8월 27일
It took some time, but here is a solution that should also work for large matrices.
clc,clear
format compact
A = [0 0 0 1 0 1 1 1 0 0 0 0 0 1]';
% 3 2 1 0 1 0 0 0 5 4 3 2 1 0
B=A;
pad=B(end)~=1;
if pad,B(end+1)=1;end %this method requires the last position to be a 1
B=flipud(B);
C=zeros(size(B));
C(B==1)=[0;diff(find(B))];
C=ones(size(B))-C;
out=cumsum(C)-1;
out=flipud(out);
if pad,out(end)=[];end
%only for display:
[A out]
  댓글 수: 4
Rik
Rik 2020년 8월 27일
If that is indeed a problem that should be easy to fix. I chose to write a comment instead of changing that behavior, but maybe I should have made it more explicit. Thank you for drawing more attention to that point.
andyc100
andyc100 2020년 8월 27일
This is not a concern for me as there will always be 1s in my implementation. Can always just do a check for not all zeros in the vector before running the code I guess.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Binbin Qi
Binbin Qi 2020년 8월 27일
A = [0 0 0 1 0 1 1 1 0 0 0 0 0 1]';
C = find(A);
D = (1:length(A)) - C;
D(D>0) = D(D>0) + inf';
min(abs(D))'
ans =
3
2
1
0
1
0
0
0
5
4
3
2
1
0
  댓글 수: 6
andyc100
andyc100 2020년 8월 27일
Thanks so much for this. Always like one liner answers, but have chosen Rik's solution as it works a lot faster for the range of rows I'm working with.

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2020년 8월 27일
편집: Bruno Luong 2020년 8월 27일
As much as I love vectorization, this problem is a typical case where the for-loop method is easier, faster, more readable.
This code is ready for 2D array, it works along the first dimension independently.
A=rand(30000,1000)>0.7;
tic
Al=logical(A);
B=zeros(size(A));
b=B(1,:);
for k=size(B,1):-1:1
b = b+1;
b(Al(k,:))=0;
B(k,:)=b;
end
toc
  댓글 수: 1
andyc100
andyc100 2020년 8월 27일
Thank you Burno. This actually works very fast too and I like that it works for multiple columns. I might end up using it if I end up needing multiple columns.

댓글을 달려면 로그인하십시오.


Bruno Luong
Bruno Luong 2020년 8월 28일
편집: Bruno Luong 2020년 8월 28일
Now I just discover CUMSUM has direction option, this is based on Rik's cumsum method, but avoid the double flipping.
B = ones(size(A));
i1 = find(A);
B(i1) = 1-diff([i1;size(A,1)+1]);
B = cumsum(B,'reverse');
  댓글 수: 1
Rik
Rik 2020년 8월 28일
Cool, I didn't remember that was an option. Turns out that is already an option as far back as R2015a. The release notes no longer allow you to look back further than R2015a, even if you try modifying the url. All I know is that it isn't possible in R2011a.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by