Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to a 1-D matrix into several 1-D matrix based on condition
Replies: 5   Last Post: Dec 8, 2012 4:36 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: How to a 1-D matrix into several 1-D matrix based on condition
Posted: Dec 8, 2012 4:05 PM

On 12/8/2012 3:02 PM, james bejon wrote:
> Can't test this, but if you want a vectorized version:
>
> A = [1 3 5 7 8 1 4 0 1 4 6 3 0 0 1 4 6 0 0 0 2 5 9 1].';
> Z = find(A == 0);
> Z = Z(diff([0; Z]) ~= 1);
> S = zeros(length(A), 1);
> S(Z) = 1;
> S = cumsum(S)+1;
> X = accumarray(S, A, [], @(x) {x})

Yeah, probably...but my version here predates accumarray() which sorta'
kills many things similar to OP's problem w/o a loop...or requires so
much other stuff as workaround as to make the loop "more better" in the
end...

I've not looked to see if Bruno or somebody did a FEX submittal...

--

Date Subject Author
12/8/12 S Ehtesham Al Hanif
12/8/12 dpb
12/8/12 james bejon
12/8/12 dpb
12/8/12 S Ehtesham Al Hanif
12/8/12 dpb