Find sorrunding elements and element from an array
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 26 Ott 2020
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
il 26 Ott 2020
Walter Roberson
il 26 Ott 2020
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
il 26 Ott 2020
Modificato: Life is Wonderful
il 26 Ott 2020
Walter Roberson
il 27 Ott 2020
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
il 27 Ott 2020
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
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Walter Roberson
il 27 Ott 2020
start = max(1, position-2);
stop = start + 4;
Now do the fix-up for end of buffer.
Life is Wonderful
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Walter Roberson
il 27 Ott 2020
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
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Walter Roberson
il 27 Ott 2020
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
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Walter Roberson
il 27 Ott 2020
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
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
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
il 27 Ott 2020
Life is Wonderful
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Adam Danz
il 27 Ott 2020
Could you show what the two outputs should be for the input in my comment?
Life is Wonderful
il 27 Ott 2020
Modificato: Life is Wonderful
il 27 Ott 2020
Adam Danz
il 27 Ott 2020
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
il 28 Ott 2020
Life is Wonderful
il 8 Nov 2020
Life is Wonderful
il 19 Nov 2020
Life is Wonderful
il 5 Dic 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Discrete in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
