Split an array using specific points

조회 수: 76 (최근 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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by