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

2 Ansichten (letzte 30 Tage)
Edward
Edward am 23 Jan. 2014
Beantwortet: Amit am 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.

Antworten (1)

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

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by