フィルターのクリア

Split an array using specific points

77 ビュー (過去 30 日間)
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 1 日
コメント済み: Stephen23 2022 年 4 月 26 日
Just starting out in matlab and would appreciate your help.
for example : lets say I have an array Y = [5;2;3;6;7;9;5;5;6;3;8;7;74;9;63;47;10;3]; and another array of index diiv = [5;9;15];
How can i use diiv to break Y into smaller arrays so I would get as output arrays y1 = [5;2;3;6;7], y2 =[9;5;5;6]; y3 =[3;8;7;74;9;63]; y4=[47;10;3]
Thanks
  1 件のコメント
Stephen23
Stephen23 2022 年 3 月 2 日
編集済み: Stephen23 2022 年 3 月 2 日
If you are numbering your variables like that then you are doing something wrong.
If you are trying to access variable names dynamically then you are doing something wrong.
Simple and efficient MATLAB code uses arrays and indexing. You should use arrays and indexing.

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

採用された回答

Stephen23
Stephen23 2022 年 3 月 2 日
編集済み: Stephen23 2022 年 3 月 2 日
Simpler and more efficient than what you are attempting (although most likely you don't realise that yet):
Y = [5;2;3;6;7;9;5;5;6;3;8;7;74;9;63;47;10;3];
diiv = [5;9;15];
D = diff([0;diiv;numel(Y)]); % complete the missing data
C = mat2cell(Y,D,1) % split into a cell array
C = 4×1 cell array
{5×1 double} {4×1 double} {6×1 double} {3×1 double}
C{:}
ans = 5×1
5 2 3 6 7
ans = 4×1
9 5 5 6
ans = 6×1
3 8 7 74 9 63
ans = 3×1
47 10 3
  2 件のコメント
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 2 日
Oh this is just perfect. I understand what you mean by using indexing. this is a better solution.
Stephen23
Stephen23 2022 年 4 月 26 日
@Daniel Adeniyi: if my answer helped you please click the accept button.

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

その他の回答 (2 件)

Alan Stevens
Alan Stevens 2022 年 3 月 1 日
Like this?
Y = [5;2;3;6;7;9;5;5;6;3;8;7;74;9;63;47;10;3];
diiv = [5; 9; 15];
y1 = Y(1:diiv(1));
y2 = Y(diiv(1)+1:diiv(2));
y3 = Y(diiv(2)+1:diiv(3));
y4 = Y(diiv(3)+1:end);
  2 件のコメント
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 2 日
Yes something like this, but is it possible to do this automatically (like in a loop) without writing out each expression for y1, y2,y3,y4
Stephen23
Stephen23 2022 年 3 月 2 日
編集済み: Stephen23 2022 年 3 月 2 日
"is it possible to do this automatically (like in a loop) without writing out each expression for y1, y2,y3,y4"
It is certainly possible, but only if you want to force yourself into writing slow, complex, inefficient code which is buggy and hard to debug:
In contrast the neat, simple, and very efficient MATLAB approach is to use indexing.
Tip: if you are numbering your varaible names like that, then you are doing something wrong.

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


Matt J
Matt J 2022 年 3 月 1 日
More likely you would want to do something like this,
y=mat2cell(Y,diiv)
  1 件のコメント
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 2 日
this gives a dimension error unfortunately as the sum of the elements in diiv is greater than the number of elements in Y.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by