Find all occuarances of two numbers together

1 回表示 (過去 30 日間)
Inna Pelloso
Inna Pelloso 2020 年 10 月 26 日
コメント済み: Image Analyst 2020 年 10 月 26 日
Hi,
I have an array, a = [ 0 -1 1 0 0 -1 1 0]
How can I fing all the ocurances of [ 1 0], ie. the index showing when the number 0 occurs after the number 1.
I want to create, b = [0 0 0 1 0 0 0 0 1].
I'm trying to use teh strfind fuction, but am stuck.
Thank you!
Inna

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 26 日
Try this
a = [ 0 -1 1 0 0 -1 1 0];
idx = strfind(a, [1 0])+1;
b = zeros(size(a));
b(idx) = 1;
  1 件のコメント
Mathieu NOE
Mathieu NOE 2020 年 10 月 26 日
yep ! it's exactly the same........:)

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

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2020 年 10 月 26 日
hello Inna
you have to test a vs [1 0] and not [0 1]
this is the code :
a = [ 0 -1 1 0 0 -1 1 0];
t = [1 0];
ind = findstr(a,t);
ind_zero = ind+1; % findstr output index is for the first term of "t" (1) => to get the index of the trailing "0" you need to add 1
b = zeros(size(a));
b(ind_zero) = 1
  2 件のコメント
Bruno Luong
Bruno Luong 2020 年 10 月 26 日
編集済み: Bruno Luong 2020 年 10 月 26 日
Attention, better use strfind and not findstr, if a = 1
>> findstr(1,[0 1]) % not expected result
ans =
2
>> strfind(1,[0 1])
ans =
[]
Image Analyst
Image Analyst 2020 年 10 月 26 日
findstr is not recommended. Use contains or strfind instead.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by