How can I create vectors of all the non-zero strings of numbers in a vector?

1 vue (au cours des 30 derniers jours)
Edward
Edward le 23 Jan 2014
Réponse apportée : Amit le 23 Jan 2014
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.

Réponses (1)

Amit
Amit le 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 )

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by