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

1 回表示 (過去 30 日間)
Edward
Edward 2014 年 1 月 23 日
回答済み: Amit 2014 年 1 月 23 日
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.

回答 (1 件)

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by