dpb
Posts:
8,231
Registered:
6/7/07


Re: How to a 1D matrix into several 1D matrix based on condition
Posted:
Dec 8, 2012 3:46 PM


On 12/8/2012 1:15 PM, S Ehtesham Al Hanif wrote: > Suppose I have matrix A like below: > ...
> > Now I want to divide these into a few groups (1 D matrix) based on one > condition that is when in that matrix i will reach 0 it will generate a > 1D matrix up to this such as below mentioned B,C,D,E. > ...
> > Can anybody suggest me how to do this?
Is there a homework problem that needs this somehow???? I just solved the same problem for a Christopher Fell in a thread " Divide single column into variables" just a few days ago....
First, for many reasons you do _NOT_ want to poof variable names into the Matlab workspacefor this problem use a cell array since the various subsets are of varying lengths. If you really, really, really want a alpha name, then put the cells into named structure fields...
For your A vector...
>> inz=find(A); >> ix=find(diff(inz)>1); >> i1=1;for i=1:length(ix),c(i)={A(inz(i1):inz(ix(i)))'};i1=ix(i)+1;end >> c(i+1)={A(inz(i1):end)'}; >> c{:} ans = 1 3 5 7 8 1 4 ans = 1 4 6 3 ans = 1 4 6 ans = 2 5 9 1 >>
Salt to suit a fixup if you do want to keep the zerosit's a relatively easy modification.
Note I transposed the column to row vector to make the final output more concise.


