Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Re: subvector without zeros

$
0
0
On 12/17/2012 9:33 PM, diana escalona wrote:
> How can I obtain vectors whenever find a zero or zeros, for example this is the input
>
> a=[16 17 32 0 0 63 79 80 0 0 0 113 0 129 130]
>
> and I want to obtain vectors and their sum
>
> a1={6 17 32 }= sum{a1}
> a2={63 79 80}= sum{a2}
> a3={113 }= sum{a3}
> a4={129 130}= sum{a4}
>
> thanks
>

This breaks the array into parts between zeros

-------------------------
a=[16 17 32 0 0 63 79 80 0 0 0 113 0 129 130];
idx=find(a==0);
r=arrayfun(@(i) a(idx(i):idx(i+1)),1:(length(idx)-1), 'UniformOutput',false);
r={a(1:idx(1)), r{:}, a(idx(end):end)};
-----------------------------

EDU>> r{:}

      16 17 32 0
      0 0
      0 63 79 80 0
      0 0
      0 0
      0 113 0
      0 129 130

I'll leave the sum of each row above for you to finish.
(hint use any() to skip rows with just zeros in them)

--Nasser

Viewing all articles
Browse latest Browse all 19628

Trending Articles