フィルターのクリア

Get the positions of the first logicals (multiple sets) in an array

1 回表示 (過去 30 日間)
BobbyRoberts
BobbyRoberts 2022 年 6 月 16 日
コメント済み: Voss 2022 年 6 月 16 日
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 件のコメント
BobbyRoberts
BobbyRoberts 2022 年 6 月 16 日
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]
BobbyRoberts
BobbyRoberts 2022 年 6 月 16 日
if you use the original x array, sorry. Not the logical. I'm converting it to logical here using x_idx = x>5.

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 6 月 16 日
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];
thresh = 5;
x_log = x>thresh
x_log = 1×20 logical array
0 0 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0
One way to get the indices of the first 1s:
idx = find(diff([false x_log]) == 1)
idx = 1×3
4 11 16
Another way:
idx = strfind([false x_log],[false true])
idx = 1×3
4 11 16
(In either case, a false/0 element is prepended to x_log to correctly handle the case that x_log starts with a true/1.)
Then, regardless of how you get idx, build x_new from idx:
x_new = false(1,numel(x));
x_new(idx) = true
x_new = 1×20 logical array
0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0
  2 件のコメント
BobbyRoberts
BobbyRoberts 2022 年 6 月 16 日
nice! thank you. very similar to what i did, but more logical (pun maybe intended).
Voss
Voss 2022 年 6 月 16 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 16 日
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
mask = 1×20 logical array
0 0 0 1 1 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0
props = regionprops(mask, 'PixelIdxList');
for k = 1 : length(props)
startingIndexes(k) = props(k).PixelIdxList(1);
end
% Show in command window:
startingIndexes
startingIndexes = 1×3
4 11 16

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by