フィルターのクリア

Making consecutive 1s and 0s as a seperate element

1 回表示 (過去 30 日間)
Jetty Rakesh Aditya
Jetty Rakesh Aditya 2020 年 10 月 17 日
編集済み: Ameer Hamza 2020 年 10 月 17 日
If I have a cell say a= [1 0 0 0 1 1 1 1 0 1] I want b= [ 1,0 0 0, 1 1 1 1,0,1]
  2 件のコメント
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 17 日
When you create vector in matlab, two elements of vector are separated by comma (,) or space. Meaning of both are same.
Jetty Rakesh Aditya
Jetty Rakesh Aditya 2020 年 10 月 17 日
I understand what you are stating but my aim is to make consecutive 1s and 0s as seperate elements in an array

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 17 日
編集済み: Ameer Hamza 2020 年 10 月 17 日
You can create a cell array
a = [1 1 0 0 0 1 1 1 1 0 1 1];
idx = [1 find(diff(a)~=0)+1 numel(a)+1];
C = cell(numel(idx)-1, 1);
for i = 1:numel(C)
C{i} = a(idx(i):idx(i+1)-1);
end
Result
>> C{1}
ans =
1 1
>> C{2}
ans =
0 0 0
>> C{3}
ans =
1 1 1 1
>> C{4}
ans =
0
>> C{5}
ans =
1 1
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 10 月 17 日
Try the updated code.
Jetty Rakesh Aditya
Jetty Rakesh Aditya 2020 年 10 月 17 日
@Ameer Hamza thanks a lot!

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

その他の回答 (2 件)

KSSV
KSSV 2020 年 10 月 17 日

Adam Danz
Adam Danz 2020 年 10 月 17 日
編集済み: Adam Danz 2020 年 10 月 17 日
a = [1 0 0 0 1 1 1 1 0 1];
consecGroups = findgroups(cumsum([true, diff(a)~=0]));
b = arrayfun(@(g){a(consecGroups==g)},1:max(consecGroups));
% Show results
celldisp(b)
b{1} = 1 b{2} = 0 0 0 b{3} = 1 1 1 1 b{4} = 0 b{5} = 1

カテゴリ

Help Center および File ExchangeLive Scripts and Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by