Get the positions of the first logicals (multiple sets) in an array
Mostrar comentarios más antiguos
Hi. I am trying to find where the value of an array increases to a particular threshold (thresh=5). For example, I started off with:
x = [0.5,1.6,1.5,5.2,8.5,9.3,6.3,2.2,1.9,1.8,7.4,6.4,6.5,2.3,2.4,8.5,7.6,5.3,2.0,2.4]
I converted this to a logical :
thresh = 5;
x_log = x>thresh;
which gives me:
x_log = [0 0 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0]
What I actually need is a logical that just has the first 1 of each group. So the output should be [0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0] ... is there a function I don't know of that can do this? I can't find it. Or do you suggest another way to do this using the original array?
Thanks a lot in advance!
4 comentarios
BobbyRoberts
el 16 de Jun. de 2022
Editada: BobbyRoberts
el 16 de Jun. de 2022
Image Analyst
el 16 de Jun. de 2022
Editada: Image Analyst
el 16 de Jun. de 2022
Why do you say it works?
x = [0 0 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0];
x_idx = x>5;
x_idx = find(ischange(double(x_idx),'Threshold',0));
x_idx(2:2:end,:) = [];
x_new = zeros(length(x),1);
x_new(x_idx) = 1
No element of x_new is 1.
See my solution below in the official "Answers" section of this page.
BobbyRoberts
el 16 de Jun. de 2022
BobbyRoberts
el 16 de Jun. de 2022
Respuesta aceptada
Más respuestas (1)
Try this:
x = [0.5,1.6,1.5,5.2,8.5,9.3,6.3,2.2,1.9,1.8,7.4,6.4,6.5,2.3,2.4,8.5,7.6,5.3,2.0,2.4];
mask = x > 5
props = regionprops(mask, 'PixelIdxList');
for k = 1 : length(props)
startingIndexes(k) = props(k).PixelIdxList(1);
end
% Show in command window:
startingIndexes
1 comentario
BobbyRoberts
el 16 de Jun. de 2022
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!