How can I create vectors of all the non-zero strings of numbers in a vector?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Amit
2014-1-23
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!