How to split or divide a larger array in to the predetermined size?
2 次查看(过去 30 天)
显示 更早的评论
My original array is only one dimensional of ordeer 1*15000 and 90% of it consisting zeroes. I want to create smaller arrays where there is non zero values and store them in different array. For example if my original array is [0,0,0,0,6,8,6,0,0,0,0,0,8,8,7,6,87,6,0,0,0] then I want it to split in [6,8,6] and [8,8,7,6,87,6].
I wrote the code to determine the starting and ending position of the non zero element but it is lengthy. Is there any other way?
0 个评论
采纳的回答
Stephen23
2016-7-5
编辑:Stephen23
2016-7-5
>> vec = [0,0,0,0,6,8,6,0,0,0,0,0,8,8,7,6,87,6,0,0,0];
>> idx = vec(:)>0;
>> idy = cumsum(diff([0;idx])>0);
>> out = accumarray(idy(idx),vec(idx),[],@(n){n});
>> out{:}
ans =
6
8
6
ans =
8
8
7
6
87
6
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!