Find sorrunding elements and element from an array
이전 댓글 표시
I have an array
y = [
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0]
where Index 7,41,75 are the locations where 1 is found .
My requirement is
- create a block around true(1) with a size of 5
- get the indices like 5,6,7,8,9 and data 0 0 1 0 0
댓글 수: 25
Walter Roberson
2020년 10월 26일
What shoud the output be if you have nearby pixels? For example
y = [
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0]
Life is Wonderful
2020년 10월 26일
Walter Roberson
2020년 10월 26일
So to confirm, you would want [0 0 1 0 1] indices [5 6 7 8 9] and [1 0 1 0 0] indices [7 8 9 10 11] ?
Anyhow, what is your question?
Life is Wonderful
2020년 10월 26일
편집: Life is Wonderful
2020년 10월 26일
Walter Roberson
2020년 10월 27일
we don't need block2 if the indices is in Block 1
So you want a single combined output that is longer, block1 = [0 0 1 0 1 0 0] indices [5 6 7 8 9 10 11] ?
Walter Roberson
2020년 10월 27일
You appear to be doing a homework assignment. When I read the homework assignment, I am left certain that you are intended to always produce blocks of 5. Index duplication and memory consumption is not a primary concern: the block of 5 is imposed by the question.
As such, there is a much easier solution to finding the indices. Hint: find() and min() and max() . You can calculate the starting and ending indices in vectorized form.
Once you have vectors of the starting and ending indices, you can loop or arrayfun to extract the contents.
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Walter Roberson
2020년 10월 27일
start = max(1, position-2);
stop = start + 4;
Now do the fix-up for end of buffer.
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Walter Roberson
2020년 10월 27일
No, padding is not needed, as long as the length of the input vector y is not less than the blocksize (5)
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Walter Roberson
2020년 10월 27일
No need to do reverse indices.
Think about the code I posted:
start = max(1, position-2);
stop = start + 4;
We started at some unknown position. We go blocksize/2 towards an edge that is of interest to us, which would normally get us the location of the edge. But we check whether taking that step would have positioned that tentative edge beyond the actual edge, and if it did then we substitute the coordinate of the actual edge instead. Then we take that (possibly substituted) coordinate and say that the other end of the block is the blocksize towards the other edge.
It is trivial to extend this to do the same logic against the other edge, detecting if we have passed the actual edge and if so moving the boundary to the actual edge and saying that the other end of the block is blocksize towards the front.
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Walter Roberson
2020년 10월 27일
All that stuff is irrelevant. You gave the definition at https://www.mathworks.com/matlabcentral/answers/626663-find-sorrunding-elements-and-element-from-a-array#comment_1087903
Though you did omit the definition for the case [0 1 x x x]
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Trying to catch up, here.
What doesn't work from this approach?
% Create example input with true at 7,9,41,75
y = false(1,101);
y([7,9,41,75]) = true
% Pad 2 false values to the beginning and end
ypad = [false(1,2), y, false(1,2)];
% Find locations of True
I = find(ypad(:)')
% Remove any values of I closer than 2-units
I([false,diff(I)<=2]) = []
% Isolate each true-value +/-2 to the right/left
% and remove the effect of padding
A = find(ypad(:)) + (-4:0)
B = ypad(A+2)
Life is Wonderful
2020년 10월 27일
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Adam Danz
2020년 10월 27일
Could you show what the two outputs should be for the input in my comment?
Life is Wonderful
2020년 10월 27일
편집: Life is Wonderful
2020년 10월 27일
Adam Danz
2020년 10월 27일
I'm still having trouble extracting the set of rules to follow to get the expected matrix. Maybe those rules are scattered about in this thread and I haven't seen them. For example, why would the first row of A' be [7 8 9 6 5]?
Life is Wonderful
2020년 10월 28일
Life is Wonderful
2020년 11월 8일
Life is Wonderful
2020년 11월 19일
Life is Wonderful
2020년 12월 5일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Discrete에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
