How to separate vector into vectors of different lengths
Show older comments
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 Comments
Stephen23
on 12 Oct 2018
"I want to separate the y vector so that y1 ... y2 ... y3 ..."
Do NOT do that. Dynamically accessing variables names is one way that beginners force themselves into writing slow, complex, buggy code. Read this to know why:
You should store the data in one array (e.g. a cell array) and use indexing.
Star Strider
on 12 Oct 2018
This could provide some context:
Star Strider
on 12 Oct 2018
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!
Accepted Answer
More Answers (1)
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 Comments
madhan ravi
on 12 Oct 2018
+1 @Stephen expert in cell
Md Shahriar Islam
on 12 Oct 2018
Edited: Md Shahriar Islam
on 12 Oct 2018
@Md Shahriar Islam: of course you get an error, because your actual data is in a column vector, whereas what you showed us in your question is a row vector. But this is easy to fix, just swap the dimensions in the mat2cell call:
cc = mat2cell(y_mean(:),diff([0,idx]),1);
Because I added the (:) to the first input, this will work with vectors of any orientation.
Md Shahriar Islam
on 12 Oct 2018
Stephen23
on 12 Oct 2018
@Md Shahriar Islam: then you will have to add the last index yourself:
dd = diff([0,idx,numel(y_mean)]);
cc = mat2cell(y_mean(:),dd,1);
Md Shahriar Islam
on 12 Oct 2018
"actually,used this previously. didnt work. still not working"
It works perfectly for me:
>> y = rand(2124288,1); % fake data
>> idx = [297121,432478,491657,656944,962731,1129612,1314841,1577916,1767257,1925084];
>> dd = diff([0,idx,numel(y)]);
>> cc = mat2cell(y(:),dd,1);
And checking the size of the vector in each cell:
>> num = cellfun(@numel,cc)
num =
297121
135357
59179
165287
305787
166881
185229
263075
189341
157827
199204
>> sum(num)
ans = 2124288
So obviously your code is buggy. But as you have not shown us any of the code you are trying, we have no way to know what mistakes you made in your code or your explanation. If you want help, please write more than just "didnt work. still not working", which tells us nothing useful and gives us no way to actually help you.
Md Shahriar Islam
on 12 Oct 2018
Edited: Stephen23
on 12 Oct 2018
Md Shahriar Islam
on 12 Oct 2018
@Md Shahriar Islam: when I run your code the only error is due to xx being a column vector (again this is unlike your question, where it is a row vector). This is also trivial to fix:
dd = diff([0;xx(:);numel(yy)]);
Giving code that runs without any error:
>> dd = diff([0;xx(:);numel(yy)]);
>> cc = mat2cell(yy(:),dd,1);
>> num = cellfun(@numel,cc)
num =
445672
119018
134472
107560
102999
127632
265177
128830
115453
191299
>> sum(num)
ans =
1738112
But I suspect that your entire algorithm is fundamentally flawed: If you are trying to detect the moment of "impact", then simply removing a fixed number of trailing idx elements might not work. Consider this example:
ind = [ 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1] % abs(..)>thresh
tvec = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16]
idx = ???
Lets assume that t_min is 6: what are the indices that you would expect to have in the output idx ?:
- idx=[3,9,15] 6-second steps, any 1 in a group allowed.
- idx=[3,15] Only the first 1 in each group is permitted.
- idx=[???] Something else? Please explain what and why.
Md Shahriar Islam
on 12 Oct 2018
@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
on 12 Oct 2018
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!