How many consecutive ones?
조회 수: 14 (최근 30일)
이전 댓글 표시
Write a function ConsecutiveOnes to detect in a logical row array of any length the locations the length of the ones sequences. If there is a single one, the location of the one should be indicated with a length of one. For example, for the input array [0 0 1 1 0 0 1] the function ConsecutiveOnes produces the output [0 0 2 0 0 0 1]; for the input array [0,0,1,1,0,0,0,1,1,1] the function ConsecutiveOnes produces the output [0 0 2 0 0 0 0 3 0 0]
I am trying a recursion function, which should be sth like
for i = 1:a
function indicator = solve(inputArray1(i))
I know this expression is wrong, but I wonder how to display an idea like this.
댓글 수: 2
Jan
2017년 11월 9일
편집: Jan
2017년 11월 9일
I do not understand the intention of these 2 lines of code. A recursive function is possible, but much more complicated then needed.
You got this homework to learn how to solve such problems. Then it would not be useful, if the solution is posted in the forum. Try it with a loop.
채택된 답변
Andrei Bobrov
2017년 11월 9일
편집: Andrei Bobrov
2017년 11월 9일
out = double(diff([~A(1);A(:)]) == 1);
v = accumarray(cumsum(out).*A(:)+1,1);
out(out == 1) = v(2:end);
or
out = zeros(size(A));
ii = strfind([0,A(:)'],[0 1]);
out(ii) = strfind([A(:)',0],[1 0]) - ii + 1;
댓글 수: 5
추가 답변 (2개)
Jan
2017년 11월 9일
편집: Jan
2017년 11월 9일
I'd start at the end of the vector and store the value. Then run a loop from length(a)-1 to 1. If the corresponding element is 0, the value of c is written to the element on the right and c is set to 0. If it is 1, c is increased by 1. After the loop the last value of c is written to the first element of the output.
a = [0,0,1,1,0,0,0,1,1,1];
c = a(end);
for k = length(a)-1:-1:1
if a(k) == 0
a(k+1) = c;
c = 0;
else
a(k+1) = 0;
c = c + 1;
end
end
a(1) = c;
Or the same with multiplications instead of the IF branches:
a = [0,0,1,1,0,0,0,1,1,1];
c = a(end);
for k = length(a)-1:-1:1
a(k+1) = c * (1 - a(k));
c = a(k) * (c + 1);
end
a(1) = c;
Well, this is ugly. Without good comments, it will take a while until its intention gets clear.
In productive code a vectorized method would be the best idea:
k = find([true, diff(a) ~= 0]); % Indices of changed values
r = zeros(size(a));
r(k) = a(k) .* diff([k, length(a)+1]); % Distance of changes
댓글 수: 6
Jan
2017년 11월 10일
@Zhuoyi Chen: Flags are thought to call admins or editors to care about contents, which conflict with the terms of use, e.g. if they are offending. You can use a comment to mention, that a solution is working.
@Andrei: Thanks. It is interesting, that your STRFIND solution gets faster, if the input contains less 1s, while the loops do not profit.
I think, although we have posted solutions of a homework, the overkill of 6 different methods has supported the OP to learn something new about Matlab. :-)
Image Analyst
2017년 11월 10일
Interested in the method I first thought of?
v = [0,0,1,1,0,0,0,1,1,1]
v2 = zeros(size(v)); % Initialize vector of same length.
props = regionprops(logical(v), 'Area', 'PixelIdxList');
for k = 1 : length(props)
v2(props(k).PixelIdxList(1)) = props(k).Area;
end
댓글 수: 2
Alexander Cranney
2018년 3월 15일
bwconncomp may offer some speed improvements over regionprops. But I didn't know of either of these remarkable functions before seeing this, so thank you!
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!