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:
A Vector of Vectors

Subject: A Vector of Vectors

From: Erik

Date: 21 Aug, 2009 23:51:05

Message: 1 of 4

Hi,

This problem could just as easily generalize to a vector of matrices. Here it is,
I am having a bit of difficulty constructing a vector of vectors of varying lengths. That is,

T1 is a mx1 vector
T2 is a nx1 vector
.
.
.
TX is a zx1 vector

where X,m,n,z are all positive integers. Ideally, if these were all the same length, I would just construct T = [T1 T2 ... TX] such that I could access Tj with T(:,j). But if these vectors are different lengths, how would I efficiently put them into one vector or cell such that I could index each vector easily? I realize I could zero pad each one appropriately, but there must be a better way.

Thanks

Subject: A Vector of Vectors

From: Matt Fig

Date: 22 Aug, 2009 00:10:20

Message: 2 of 4

"Erik" <emiehling@gmail.com> wrote in message <h6nbt8$76v$1@fred.mathworks.com>...
> Hi,
>
> This problem could just as easily generalize to a vector of matrices. Here it is,
> I am having a bit of difficulty constructing a vector of vectors of varying lengths. That is,
>
> T1 is a mx1 vector
> T2 is a nx1 vector
> .
> .
> .
> TX is a zx1 vector
>
> where X,m,n,z are all positive integers. Ideally, if these were all the same length, I would just construct T = [T1 T2 ... TX] such that I could access Tj with T(:,j). But if these vectors are different lengths, how would I efficiently put them into one vector or cell such that I could index each vector easily? I realize I could zero pad each one appropriately, but there must be a better way.
>
> Thanks


Look into cell arrays. I.e.,

>>docsearch('Cell Arrays')

Subject: A Vector of Vectors

From: James Tursa

Date: 22 Aug, 2009 00:17:02

Message: 3 of 4

"Erik" <emiehling@gmail.com> wrote in message <h6nbt8$76v$1@fred.mathworks.com>...
> Hi,
>
> This problem could just as easily generalize to a vector of matrices. Here it is,
> I am having a bit of difficulty constructing a vector of vectors of varying lengths. That is,
>
> T1 is a mx1 vector
> T2 is a nx1 vector
> .
> .
> .
> TX is a zx1 vector
>
> where X,m,n,z are all positive integers. Ideally, if these were all the same length, I would just construct T = [T1 T2 ... TX] such that I could access Tj with T(:,j). But if these vectors are different lengths, how would I efficiently put them into one vector or cell such that I could index each vector easily? I realize I could zero pad each one appropriately, but there must be a better way.
>
> Thanks

T = {T1,T2,...,TX}

To get at one of the vectors, use the { } notation. e.g.,

T{1} would get you T1, etc.

James Tursa

Subject: A Vector of Vectors

From: Ambarish Jash

Date: 22 Aug, 2009 00:18:03

Message: 4 of 4

consider 2 vectors a and b of different sizes.
try this
a = {a}; %converting to type cell
b = {b};
c = [a b]; %storing in a vector c

%to access
xx = c(1) % accesses a but still a cell structure
xx = cell2mat(xx);
%and you are done

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