How can I create vectors of all the non-zero strings of numbers in a vector?
2 views (last 30 days)
Show older comments
Given a vector that looks something like [0 0 3 4 2 2 0 0 1 0 0 2 4 0], how can I pull out the nonzero values to get three vectors: [3 4 2 2], [1], and [2 4]? I'm not sure how to create a function that will do this.
0 Comments
Answers (1)
Amit
on 23 Jan 2014
Lets call you vector as A
I = find(A);
a = diff(I);
b = find([a inf] > 1);
c = diff([0 b]);
d = cumsum(c);
d = [0 d];
Ax = struct;
for i = 1:length(d) - 1
Ax.Y{i} = A(I(d(i)+1:d(i+1)));
end
Ax.Y has all the three vector. The consecutive finding algorithm credit goes to Laurent ( http://www.mathworks.com/matlabcentral/answers/86420-find-a-series-of-consecutive-numbers-in-a-vector )
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!