How to separate vector into vectors of different lengths

1 回表示 (過去 30 日間)
Md Shahriar Islam
Md Shahriar Islam 2018 年 10 月 12 日
コメント済み: Md Shahriar Islam 2018 年 10 月 12 日
I have a vector y=[1:1:10] and length vector l=[2,5,10] % these are the sample number of the vector y; I want to separate the y vector so that y1=[1,2]; y2=[3,4,5]; y3=[6,7,8,9,10];
  4 件のコメント
Stephen23
Stephen23 2018 年 10 月 12 日
編集済み: Stephen23 2018 年 10 月 12 日
Uh ... facepalm
Thank you Star Strider. If I had looked I would have saved myself reinventing the wheel :)
Star Strider
Star Strider 2018 年 10 月 12 日
My pleasure, Stephen. I posted my Answer 6 days (and 1115 Questions) before this one, so it would have been difficult to find.
I probably should have made my explanation a bit more obvious, and emphasized that ‘Out’ was the desired result, although I thought it would be self-explanatory as I posted it.
Live and learn!

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

採用された回答

KSSV
KSSV 2018 年 10 月 12 日
y=[1:1:10] ;
l=[2,5,10] ;
y1=[1,2] ;
y2=[3,4,5] ;
y3=[6,7,8,9,10] ;
iwant = cell(length(l),1) ;
iwant{1} = y(1:l(1)) ;
for i = 2:length(l)
iwant{i} = y(l(i-1)+1:l(i)) ;
end
  1 件のコメント
Md Shahriar Islam
Md Shahriar Islam 2018 年 10 月 12 日
This works fine. But facing an issue with the actual data when I have a situation of similar to l=[2,5,9] here I would like to collect data in between the lengths 2to5; 5to9; and 9to10

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 10 月 12 日
編集済み: Stephen23 2018 年 10 月 12 日
The simple MATLAB way:
>> y = 1:10;
>> x = [2,5,10];
>> c = mat2cell(y,1,diff([0,x]));
>> c{:}
ans =
1 2
ans =
3 4 5
ans =
6 7 8 9 10
  13 件のコメント
Stephen23
Stephen23 2018 年 10 月 12 日
編集済み: Stephen23 2018 年 10 月 12 日
@Md Shahriar Islam: You missed my point. The question is: do you want the values in idx to represent the first 1 in a sequence of 1's, or do you want to detect any 1 in a sequence of 1's?
The same question in different words: what are the indices in idx supposed to represent? Are they supposed to represent the start of each "impact".
Please read my previous comment again, and tell my what indices you would expect for the small example I gave. This is very important to know exactly what you mean when you say that you want "to separate impacts".
Md Shahriar Islam
Md Shahriar Islam 2018 年 10 月 12 日
@Stephen Cobeldick: sorry, missed your point.
Yes. I want values in idx to represent 1 in a sequence of 1's.
idx=[3,8,15]

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

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by