how to separate the non-zero elements from an array

suppose i have a column matrix with non zero elements in between the zeros. i want to put the nonzero values in a cell of different groups of nonzero integers.
for example x=[ 0 0 0 1 2 3 0 0 2 3 5 6 0 0 2 5 0]
[1 2 3] is one group [2 3 5 6 ] is the next. i want all these groups in one cell.

 採用された回答

KSSV
KSSV 2018 年 11 月 28 日

0 投票

A = [ 0 0 0 1 2 3 0 0 2 3 5 6 0 0 2 5 0] ;
ii = zeros(size(A));
jj = A > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj)',A(jj)',[],@(x){x'});

5 件のコメント

johnson saldanha
johnson saldanha 2018 年 11 月 28 日
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
Error in accelerationrate (line 12)
out = accumarray( idx(jj)',x(jj)',[],@(x){x'});
im getting this error
johnson saldanha
johnson saldanha 2018 年 11 月 29 日
could u help me with that error?
KSSV
KSSV 2018 年 11 月 29 日
Show me the whole code..you tried with input.
johnson saldanha
johnson saldanha 2018 年 11 月 29 日
T=readtable('finalcycle.xlsx');
data2=[T(:,1),T(:,2),T(:,3),T(:,4)];
data1=table2array(data2);
v=data1(:,2);
ii = zeros(size(v));
jj = v > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
out = accumarray( idx(jj),v(jj),[],@(x){x'});
johnson saldanha
johnson saldanha 2018 年 11 月 29 日
or is it possible for me to put it in a matrix. the first column being the first group, second column being the second group and so on

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by