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:
Combining various sized arrays into one column

Subject: Combining various sized arrays into one column

From: D

Date: 11 Jun, 2010 18:15:37

Message: 1 of 5

Hello all,

I feel ashamed for asking a beginner question but I haven't been able to develop the right idea myself - I hope to get direction on which functions to use to accomplish what I need to do.

I have a huge array, A with size (r x c). I have conditional statements to pick out certain data from A, and it is returning several arrays of various sizes - like B (r1 x c1), C (r2 x c2), D (r3 x c3)...

I was able to make the returned arrays into a single column, i.e. B (1:end,1) but my challenge is combining the returned single column data sets (B, C, D...) into a final single column array, FINAL=[B;C;D...so on].

I hope this makes sense. Thank you for reading.

- Dale

Subject: Combining various sized arrays into one column

From: dpb

Date: 11 Jun, 2010 18:25:48

Message: 2 of 5

D wrote:
...

> I feel ashamed for asking a beginner question but I haven't been able to
> develop the right idea myself - I hope to get direction on which
> functions to use to accomplish what I need to do.

No need; we all had to start from somewhere...

> I have a huge array, A with size (r x c). I have conditional statements
> to pick out certain data from A, and it is returning several arrays of
> various sizes - like B (r1 x c1), C (r2 x c2), D (r3 x c3)...
>
> I was able to make the returned arrays into a single column, i.e. B(1:end,1) ...

Actually, you can do that in more concise notation that is identical in
function as

B(:)

> but my challenge is combining the returned single column data
> sets (B, C, D...) into a final single column array, FINAL=[B;C;D...so on].
...

> I hope this makes sense. Thank you for reading.
...

Well, I was with you until I got to

FINAL=[B;C;D...so on]

since that will do what you ask once you have the individual arrays as
column vectors.

So, where are you stuck and a more precise demonstration of what your
existing code is might help.

Until then, see

doc vertcat

Note that one can repetitively do something like

V = B;
V = [V;C];
...
etc.

in code or one could do something similar excepting using the result of
the search operation directly and avoid the intermediate or one could
use the indices of a find() and build the array directly from them or...

There are (as almost always) so many choices from which to choose
depending on the exact situation and requirements...

--

Subject: Combining various sized arrays into one column

From: D

Date: 11 Jun, 2010 21:38:04

Message: 3 of 5

Thank you for the response. I worked on the code with a different approach, and I think the problem now lies within my for-loop. I hope this will clarify:

ind =
    76 93 177 197 235 256 328 350 388 406 476 498

size(ank) =
   537 3

for st=1:length(ind)-1;
    ank_x=ank(ind(st):ind(st+1),1)
end

Now the challenge is that for each st, ank_x is spitting out individual arrays with each as a single column and with various number of rows. How do I automatically capture each of the ank_x results, and stack them upon each other as an array with a single column?



dpb <none@non.net> wrote in message <hutv84$qbk$1@news.eternal-september.org>...
> D wrote:
> ...
>
> > I feel ashamed for asking a beginner question but I haven't been able to
> > develop the right idea myself - I hope to get direction on which
> > functions to use to accomplish what I need to do.
>
> No need; we all had to start from somewhere...
>
> > I have a huge array, A with size (r x c). I have conditional statements
> > to pick out certain data from A, and it is returning several arrays of
> > various sizes - like B (r1 x c1), C (r2 x c2), D (r3 x c3)...
> >
> > I was able to make the returned arrays into a single column, i.e. B(1:end,1) ...
>
> Actually, you can do that in more concise notation that is identical in
> function as
>
> B(:)
>
> > but my challenge is combining the returned single column data
> > sets (B, C, D...) into a final single column array, FINAL=[B;C;D...so on].
> ...
>
> > I hope this makes sense. Thank you for reading.
> ...
>
> Well, I was with you until I got to
>
> FINAL=[B;C;D...so on]
>
> since that will do what you ask once you have the individual arrays as
> column vectors.
>
> So, where are you stuck and a more precise demonstration of what your
> existing code is might help.
>
> Until then, see
>
> doc vertcat
>
> Note that one can repetitively do something like
>
> V = B;
> V = [V;C];
> ...
> etc.
>
> in code or one could do something similar excepting using the result of
> the search operation directly and avoid the intermediate or one could
> use the indices of a find() and build the array directly from them or...
>
> There are (as almost always) so many choices from which to choose
> depending on the exact situation and requirements...
>
> --

Subject: Combining various sized arrays into one column

From: dpb

Date: 11 Jun, 2010 22:04:24

Message: 4 of 5

D wrote:
...[top posting repaired--please don't do that hard thread follow makes]...
> dpb <none@non.net> wrote in message
> <hutv84$qbk$1@news.eternal-september.org>...
>> D wrote:
>> ...
>>
...
>> Well, I was with you until I got to
>>
>> FINAL=[B;C;D...so on]
>>
>> since that will do what you ask once you have the individual arrays as
>> column vectors.
>>
>> So, where are you stuck and a more precise demonstration of what your
>> existing code is might help.
...

>> Note that one can repetitively do something like
>> V = B;
>> V = [V;C];
>> ...
>> etc.
>>
...
 > Thank you for the response. I worked on the code with a different
 > approach, and I think the problem now lies within my for-loop. I hope
 > this will clarify:
 >
 > ind =
 > 76 93 177 197 235 256 328 350 388 406 476 498
 >
 > size(ank) = 537 3
 >
 > for st=1:length(ind)-1;
 > ank_x=ank(ind(st):ind(st+1),1)
 > end
 >
> Now the challenge is that for each st, ank_x is spitting out
> individual arrays with each as a single column and with various
> number of rows.
> How do I automatically capture each of the ank_x results, and stack
> them upon each other as an array with a single column?

The sample I showed above would do that...
  ank_x=[]; % create empty array
  for st=1:length(ind)-1;
    ank_x=[ank_x; ank(ind(st):ind(st+1),1)];
  end

You could get fancy and precompute the length of the end result since
it's known by the number of pairs and difference in them and preallocate
the ank_x array and index into it. For relatively small sizes as
dealing with here it wouldn't make too much difference; it will as sizes
get much larger or if the operation were nested in other iterative
loops, etc.

For example,

ank_x = zeros(sum(diff(ind)+length(ind)-1),1); % preallocate
i1 = ind(1);
for st = 2:length(ind);
   i2 = ind(st);
   ank_x(i1:i2) = ank(i1:i2,1);
   i1=i2;
end

--

Subject: Combining various sized arrays into one column

From: dpb

Date: 13 Jun, 2010 16:33:37

Message: 5 of 5

dpb wrote:
...
> You could get fancy and precompute the length of the end result since
> it's known by the number of pairs and difference in them ...
>
> ank_x = zeros(sum(diff(ind)+length(ind)-1),1); % preallocate
...
which is written a little more neatly as

ank_x = zeros(sum(diff(ind)+1),1); % preallocate,

of course...

--

Tags for 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