Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Divide single column into variables

Subject: Divide single column into variables

From: Christopher Fell

Date: 30 Nov, 2012 18:27:09

Message: 1 of 9

I have a single column / row of data that I would like to separate into multiple columns or variables. The column is current 59000 data points. The output lengths of the columns will vary as the data is different lengths. The data is separated by zero's and is typically linearly increasing (1,2,3,4) however there are some points that decrease by fractional amounts (1, 2, 1.8, 3). I tried using the FIND command to identify the zero's and a for/if loop to extract the data into columns but was not successful. Any suggestions are much appreciated. Some example data looks like:
0.1, 0.2, 0.18 ,0.3 ,0.4, 0, 0, 0, 0, 0.8, 0.75, 0.9, 0, 0, 0, 0, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, etc

Subject: Divide single column into variables

From: dpb

Date: 30 Nov, 2012 19:01:09

Message: 2 of 9

On 11/30/2012 12:27 PM, Christopher Fell wrote:
> I have a single column / row of data that I would like to separate into
> multiple columns or variables. The column is current 59000 data points.
> The output lengths of the columns will vary as the data is different
> lengths. The data is separated by zero's and is typically linearly
> increasing (1,2,3,4) however there are some points that decrease by
> fractional amounts (1, 2, 1.8, 3). I tried using the FIND command to
> identify the zero's and a for/if loop to extract the data into columns
> but was not successful. Any suggestions are much appreciated. Some
> example data looks like:
> 0.1, 0.2, 0.18 ,0.3 ,0.4, 0, 0, 0, 0, 0.8, 0.75, 0.9, 0, 0, 0, 0, 1.3,
> 1.4, 1.5, 1.6, 1.7, 1.8, etc

Well, if the column lengths aren't going to be equal then you'll have to
use cell arrays--regular arrays can only have consistent rows/columns;
they can't be "jagged arrays".

For the data above, what do you want to do w/ the multiple zeros? Give
the expected desired output for the input.

--

Subject: Divide single column into variables

From: Christopher Fell

Date: 30 Nov, 2012 19:50:08

Message: 3 of 9

dpb <none@non.net> wrote in message <k9avpk$8c3$1@speranza.aioe.org>...
> On 11/30/2012 12:27 PM, Christopher Fell wrote:
> > I have a single column / row of data that I would like to separate into
> > multiple columns or variables. The column is current 59000 data points.
> > The output lengths of the columns will vary as the data is different
> > lengths. The data is separated by zero's and is typically linearly
> > increasing (1,2,3,4) however there are some points that decrease by
> > fractional amounts (1, 2, 1.8, 3). I tried using the FIND command to
> > identify the zero's and a for/if loop to extract the data into columns
> > but was not successful. Any suggestions are much appreciated. Some
> > example data looks like:
> > 0.1, 0.2, 0.18 ,0.3 ,0.4, 0, 0, 0, 0, 0.8, 0.75, 0.9, 0, 0, 0, 0, 1.3,
> > 1.4, 1.5, 1.6, 1.7, 1.8, etc
>
> Well, if the column lengths aren't going to be equal then you'll have to
> use cell arrays--regular arrays can only have consistent rows/columns;
> they can't be "jagged arrays".
>
> For the data above, what do you want to do w/ the multiple zeros? Give
> the expected desired output for the input.
>
> --

Ideally the zero's should be eliminated.
Desired Output:
C1=0.1, 0.2, 0.18 ,0.3 ,0.4
C2=0.8, 0.75, 0.9
C3=1.3, 1.4, 1.5, 1.6, 1.7, 1.8

Subject: Divide single column into variables

From: Christopher Fell

Date: 30 Nov, 2012 19:53:08

Message: 4 of 9

dpb <none@non.net> wrote in message <k9avpk$8c3$1@speranza.aioe.org>...
> On 11/30/2012 12:27 PM, Christopher Fell wrote:
> > I have a single column / row of data that I would like to separate into
> > multiple columns or variables. The column is current 59000 data points.
> > The output lengths of the columns will vary as the data is different
> > lengths. The data is separated by zero's and is typically linearly
> > increasing (1,2,3,4) however there are some points that decrease by
> > fractional amounts (1, 2, 1.8, 3). I tried using the FIND command to
> > identify the zero's and a for/if loop to extract the data into columns
> > but was not successful. Any suggestions are much appreciated. Some
> > example data looks like:
> > 0.1, 0.2, 0.18 ,0.3 ,0.4, 0, 0, 0, 0, 0.8, 0.75, 0.9, 0, 0, 0, 0, 1.3,
> > 1.4, 1.5, 1.6, 1.7, 1.8, etc
>
> Well, if the column lengths aren't going to be equal then you'll have to
> use cell arrays--regular arrays can only have consistent rows/columns;
> they can't be "jagged arrays".
>
> For the data above, what do you want to do w/ the multiple zeros? Give
> the expected desired output for the input.
>
Ideally the output would eliminate the zeros and would look like
C1=0.1, 0.2, 0.18 ,0.3 ,0.4
C2=0.8, 0.75, 0.9
C3=1.3, 1.4, 1.5, 1.6, 1.7, 1.8

Subject: Divide single column into variables

From: dpb

Date: 30 Nov, 2012 22:17:31

Message: 5 of 9

On 11/30/2012 12:27 PM, Christopher Fell wrote:
> I have a single column / row of data that I would like to separate into
> multiple columns or variables. The column is current 59000 data points.
> The output lengths of the columns will vary as the data is different
> lengths. The data is separated by zero's and is typically linearly
> increasing (1,2,3,4) however there are some points that decrease by
> fractional amounts (1, 2, 1.8, 3). I tried using the FIND command to
> identify the zero's and a for/if loop to extract the data into columns
> but was not successful. Any suggestions are much appreciated. Some
> example data looks like:
> 0.1, 0.2, 0.18 ,0.3 ,0.4, 0, 0, 0, 0, 0.8, 0.75, 0.9, 0, 0, 0, 0, 1.3,
> 1.4, 1.5, 1.6, 1.7, 1.8, etc

Ok, basic steps...

inz=find(x); % locate the nonzero values
ix=find(diff(inz)>0); % find the gaps but keep locations
c=cell(length(ix)+1,1); % make an empty cell array of right size
i1=1;
for i=1:length(ix)
   c(i)={x(inz(i1):inz(ix(i)))}; % indirect address of sequence
   i1=inz(ix(i))+1; % update first index
end
c(i+1)={x(inz(i1:end)};

Subject: Divide single column into variables

From: dpb

Date: 2 Dec, 2012 21:14:30

Message: 6 of 9

On 11/30/2012 4:17 PM, dpb wrote:
...

> inz=find(x); % locate the nonzero values
> ix=find(diff(inz)>0); % find the gaps but keep locations
> c=cell(length(ix)+1,1); % make an empty cell array of right size
> i1=1;
> for i=1:length(ix)
> c(i)={x(inz(i1):inz(ix(i)))}; % indirect address of sequence
> i1=inz(ix(i))+1; % update first index
> end
> c(i+1)={x(inz(i1:end)};
>

BTW, if there are any trailing zeros at the end of the starting vector,
delete them before starting the above so want to keep the values to the
end for the final segment.

--

Subject: Divide single column into variables

From: Christopher Fell

Date: 3 Dec, 2012 15:23:10

Message: 7 of 9

dpb <none@non.net> wrote in message <k9ggbm$5bd$1@speranza.aioe.org>...
> On 11/30/2012 4:17 PM, dpb wrote:
> ...
>
> > inz=find(x); % locate the nonzero values
> > ix=find(diff(inz)>0); % find the gaps but keep locations
> > c=cell(length(ix)+1,1); % make an empty cell array of right size
> > i1=1;
> > for i=1:length(ix)
> > c(i)={x(inz(i1):inz(ix(i)))}; % indirect address of sequence
> > i1=inz(ix(i))+1; % update first index
> > end
> > c(i+1)={x(inz(i1:end)};
> >
>
> BTW, if there are any trailing zeros at the end of the starting vector,
> delete them before starting the above so want to keep the values to the
> end for the final segment.
>
> --

There was an error when I tried to execute the code for the above example set of data. There are 22 data points. When I executed the code, I received the error:
"Attempted to access inz(18); index out of bounds because numel(inz)=14."
i=1x1, value:10
inz=<1x14 double>, value:
1
2
3
4
5
10
11
12
17
18
19
20
21
22

ix=<1x13 double>, value: 1,2,3,4,5...13
i1=1x1, value: 18
c=<14x1 cell> value:
0.1
0.2
0.18
0.3
0.4
0.8
[]
[]
[]
[]
[]
[]
[]
[]

Subject: Divide single column into variables

From: dpb

Date: 3 Dec, 2012 17:43:14

Message: 8 of 9

On 12/3/2012 9:23 AM, Christopher Fell wrote:
> dpb <none@non.net> wrote in message <k9ggbm$5bd$1@speranza.aioe.org>...
>> On 11/30/2012 4:17 PM, dpb wrote:
>> ...
>>
>> > inz=find(x); % locate the nonzero values
>> > ix=find(diff(inz)>0); % find the gaps but keep locations
>> > c=cell(length(ix)+1,1); % make an empty cell array of right size
>> > i1=1;
>> > for i=1:length(ix)
>> > c(i)={x(inz(i1):inz(ix(i)))}; % indirect address of sequence
>> > i1=inz(ix(i))+1; % update first index
>> > end
>> > c(i+1)={x(inz(i1:end)};
>> >
>>
>> BTW, if there are any trailing zeros at the end of the starting
>> vector, delete them before starting the above so want to keep the
>> values to the end for the final segment.
>>
>
> There was an error when I tried to execute the code for the above
> example set of data....

ix=find(diff(inz)>1); % find the gaps but keep locations

Sorry, I retyped instead of cut'n paste...

Work through it at the command line watching the intermediate results to
get the idea....salt to suit.

 >> inz=find(x);
 >> ix=find(diff(inz)>1);
 >> i1=1;
 >> for i=1:2,c(i)={x(inz(i1):inz(ix(i)))};i1=inz(ix(i))+1;end
 >> c(i+1)={x(inz(i1:end))};
 >> c{:}
ans =
     0.1000 0.2000 0.1800 0.3000 0.4000
ans =
     0.8000 0.7500 0.9000
ans =
     1.7000 1.8000
 >>

--

Subject: Divide single column into variables

From: dpb

Date: 3 Dec, 2012 18:01:56

Message: 9 of 9

On 12/3/2012 11:43 AM, dpb wrote:
...

> >> inz=find(x);
> >> ix=find(diff(inz)>1);
> >> i1=1;

...dang--another typo...indirection in i1 not wanted; that's what ix
is...the beginning index in inz for the group so the next start is that
point plus 1...

 >> for i=1:length(x)
      c(i)={x(inz(i1):inz(ix(i)))};
      i1=(ix(i))+1;
end
 >> c(i+1)={x(inz(i1:end))};
 >> c{:}
ans =
     0.1000 0.2000 0.1800 0.3000 0.4000
ans =
     0.8000 0.7500 0.9000
ans =
     1.3000 1.4000 1.5000 1.6000 1.7000 1.8000

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us