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!
