フィルターのクリア

How to split command signal arrays in command order sequence?

1 回表示 (過去 30 日間)
jamin kong
jamin kong 2017 年 10 月 19 日
コメント済み: jamin kong 2017 年 10 月 20 日
Hello I have example for this question.
A = [0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0];
then I wanna split this signal in order sequence like below
a1 = [0 0];
a2 = [1 1 1];
a3 = [0 0];
a4 = [1 1 1 1 1];
a5 = [0 0 0];
a6 = [1 1 1];
a7 = [0 0];
It's important array number(1 ~ 7) because I will analize with order sequence.
help me anybody please..
and I built code for calculate order count number with imputing signal, I would like to attach a note for you.
A = [0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0];
a = diff(A);
b = abs(a);
c = cumsum(b);
count_order_A = c(end) + 1;
% Then you can return answer '7'
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 19 日
編集済み: Stephen23 2017 年 10 月 19 日
@jamin kong: creating or accessing numbered variables, like a1, a2, a3, etc, is slow, complex, and buggy. You should avoid doing this, and simply learn how to use fast and efficient arrays with indexing. Read this to know more:

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

採用された回答

Stephen23
Stephen23 2017 年 10 月 19 日
編集済み: Stephen23 2017 年 10 月 19 日
Use mat2cell and then cell array indexing to access the subvectors:
>> A = [0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0];
>> D = diff(find([1,diff(A),1]));
>> C = mat2cell(A,1,D);
>> C{:}
ans =
0 0
ans =
1 1 1
ans =
0 0
ans =
1 1 1 1 1
ans =
0 0 0
ans =
1 1 1
ans =
0 0
>>
  1 件のコメント
jamin kong
jamin kong 2017 年 10 月 20 日
Thank you for your awesome code!!

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

その他の回答 (1 件)

Guillaume
Guillaume 2017 年 10 月 19 日
I'm not entirely sure what you're asking, but if you want to split that vector, it's simply:
a = mat2cell(A, 1, diff([0, find(diff(A)), numel(A)]));
celldisp(a)
Note that this creates a cell array of vectors, that you can easily access with a{1}, a{2}, etc. Never ever create numbered variables as your a1, a2, ... It's dead easy to iterate over the elements of a cell array, it's hard to do the same with numbered variables.
  1 件のコメント
jamin kong
jamin kong 2017 年 10 月 20 日
Thank you for your awesome Code!!!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!