Got Questions? Get Answers.
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:
Array of array

Subject: Array of array

From: CHINEDU

Date: 26 Dec, 2012 02:14:10

Message: 1 of 15

Hi friends,
Please kindly assist me with an answer to this question:

Is it possible to create a Matlab array whose elements are in turn 3 element arrays?
I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4), etc}. If possible, how do I access the elements of this array. If not, could this be possible in Fortran?

Thanks.

Subject: Array of array

From: Nasser M. Abbasi

Date: 26 Dec, 2012 02:44:06

Message: 2 of 15

On 12/25/2012 8:14 PM, CHINEDU wrote:
> Hi friends,
> Please kindly assist me with an answer to this question:
>
> Is it possible to create a Matlab array whose elements are in turn 3 element arrays?
> I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4), etc}. If possible,
>how do I access the elements of this array. If not, could this be possible in Fortran?
>
> Thanks.
>


A=[1 2 2;
    3 2 1;
    5 3 4]

to access this matrix, use index. Like A(2,2) which will return 2 in
this example,etc...

--Nasser
  

Subject: Array of array

From: dpb

Date: 26 Dec, 2012 05:37:11

Message: 3 of 15

On 12/25/2012 8:14 PM, CHINEDU wrote:
> Hi friends,
> Please kindly assist me with an answer to this question:
>
> Is it possible to create a Matlab array whose elements are in turn 3
> element arrays?

Sure...

> I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4),
> etc}. If possible, how do I access the elements of this array. If not,
> could this be possible in Fortran?

Almost there...just have to use [] for the arrays inside the cell array
curlies...

 >> A = {[1,2,2], [3,2,1], [5,3,4]}
A =
     [1x3 double] [1x3 double] [1x3 double]

To address element contents, use them as well...

 >> A{2}(3)
ans =
      1
 >> A{:}
ans =
      1 2 2
ans =
      3 2 1
ans =
      5 3 4
 >> A{1}
ans =
      1 2 2
 >> A{1}(2)
ans =
      2
 >>

As a native structure, Fortran doesn't support cell arrays. You could
do something similar w/ user-defined types and allocatables but they
would look more like structures in Matlab than directly mimicking cell
arrays.

--

Subject: Array of array

From: Bruno Luong

Date: 26 Dec, 2012 08:11:08

Message: 4 of 15

"CHINEDU" wrote in message <kbdmhi$53h$1@newscl01ah.mathworks.com>...
> Hi friends,
> Please kindly assist me with an answer to this question:
>
> Is it possible to create a Matlab array whose elements are in turn 3 element arrays?
> I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4), etc}. If possible, how do I access the elements of this array. If not, could this be possible in Fortran?

If yours subarrays have the same size, e..g, (1x3) in the example you provide, then use standard array to store them as Nasser suggests. Avoid using CELL when you don't need it. It will be slow to access to elements and it will be hard to vectorize the code.

Bruno

Subject: Array of array

From: CHINEDU

Date: 26 Dec, 2012 10:33:08

Message: 5 of 15

dpb <none@non.net> wrote in message <kbe2d8$727$1@speranza.aioe.org>...
> On 12/25/2012 8:14 PM, CHINEDU wrote:
> > Hi friends,
> > Please kindly assist me with an answer to this question:
> >
> > Is it possible to create a Matlab array whose elements are in turn 3
> > element arrays?
>
> Sure...
>
> > I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4),
> > etc}. If possible, how do I access the elements of this array. If not,
> > could this be possible in Fortran?
>
> Almost there...just have to use [] for the arrays inside the cell array
> curlies...
>
> >> A = {[1,2,2], [3,2,1], [5,3,4]}
> A =
> [1x3 double] [1x3 double] [1x3 double]
>
> To address element contents, use them as well...
>
> >> A{2}(3)
> ans =
> 1
> >> A{:}
> ans =
> 1 2 2
> ans =
> 3 2 1
> ans =
> 5 3 4
> >> A{1}
> ans =
> 1 2 2
> >> A{1}(2)
> ans =
> 2
> >>
>
> As a native structure, Fortran doesn't support cell arrays. You could
> do something similar w/ user-defined types and allocatables but they
> would look more like structures in Matlab than directly mimicking cell
> arrays.
>
> --


Thanks all. I appreciate your quick answers.

 dpb, your answer is exactly what I wanted. I actually want to create a 2 dimensional array whose elements are in turn 3-element arrays i.e. To create a matrix A with
Row 1 = (1,2,3), (2,3,4), (2,4,5)
Row 2 = (4,5,6), (1,2,2), (0,2,1) etc

dpb's answer has answered it exactly but I am still having problem of accessing the elements. I have understood how to access the one-row type through dpo's answer but the 2D access is not still clear. I mean, how can I access elements of 2D array whose elements are subarrays? dpo please, help.

I have done something like this A ={[1,2,3],[0,-1,4];[-2,-3,-4],[9,8,7]}
And am accessing the elements using
A{r,c}(i) for the i-th element of the r-th column and c-th column eg. A{1,2}(2)=-1
Is this correct?

Subject: Array of array

From: CHINEDU

Date: 26 Dec, 2012 11:07:07

Message: 6 of 15

"CHINEDU" wrote in message <kbdmhi$53h$1@newscl01ah.mathworks.com>...
> Hi friends,
> Please kindly assist me with an answer to this question:
>
> Is it possible to create a Matlab array whose elements are in turn 3 element arrays?
> I mean, how can I create an array as A = {(1,2,2), (3,2,1), (5,3,4), etc}. If possible, how do I access the elements of this array. If not, could this be possible in Fortran?
>
> Thanks.




dpb, please kindly let me know how to initialize these Array of arrays.

Subject: Array of array

From: dpb

Date: 26 Dec, 2012 15:02:04

Message: 7 of 15

On 12/26/2012 4:33 AM, CHINEDU wrote:
...

> dpb, your answer is exactly what I wanted. I actually want to create a 2
> dimensional array whose elements are in turn 3-element arrays i.e. To
> create a matrix A with
> Row 1 = (1,2,3), (2,3,4), (2,4,5)
> Row 2 = (4,5,6), (1,2,2), (0,2,1) etc

If you're going to be using regular arrays (in size), then as Bruno and
Nasser note it'll be much simpler (as you're discovering w/ the
addressing syntax issues) and Matlab will be far more responsive to use
normal arrays instead of cell arrays.

Why the insistence on cell arrays--what's the problem domain/usage?
Perhaps a better data structure would present itself if others knew the
intent here...

> dpb's answer has answered it exactly but I am still having problem of
> accessing the elements. I have understood how to access the one-row type
> through dpo's answer but the 2D access is not still clear. I mean, how
> can I access elements of 2D array whose elements are subarrays? dpo
> please, help.
>
> I have done something like this A ={[1,2,3],[0,-1,4];[-2,-3,-4],[9,8,7]}
> And am accessing the elements using
> A{r,c}(i) for the i-th element of the r-th column and c-th column eg.
> A{1,2}(2)=-1
> Is this correct?

Yes, to get to a cell content, address the cell in curlies first, then
the element(s) in normal parens after.

doc paren

and look for "multi-level addressing" as a subject in the documentation
for more details...

--

Subject: Array of array

From: dpb

Date: 26 Dec, 2012 15:06:17

Message: 8 of 15

On 12/26/2012 5:07 AM, CHINEDU wrote:
...

> dpb, please kindly let me know how to initialize these Array of arrays.

a) initialize them before creating the cell array from them, or

b) use the same multi-level addressing syntax to specify the location of
the target for the rhs in any normal Matlab assignment.

See other note/response on more info on multi-level addressing and the
relative simplicity of the straightforward array vis a vis this solution...

--

Subject: Array of array

From: CHINEDU

Date: 28 Dec, 2012 18:05:07

Message: 9 of 15

dpb <none@non.net> wrote in message <kbf3o1$n5s$1@speranza.aioe.org>...
> On 12/26/2012 5:07 AM, CHINEDU wrote:
> ...
>
> > dpb, please kindly let me know how to initialize these Array of arrays.
>
> a) initialize them before creating the cell array from them, or
>
> b) use the same multi-level addressing syntax to specify the location of
> the target for the rhs in any normal Matlab assignment.
>
> See other note/response on more info on multi-level addressing and the
> relative simplicity of the straightforward array vis a vis this solution...
>
> --
dpb, I know I can initialize a matrix using A = zeros(20,30)
       how do I initialize a matrix of subarrays?


I actually want to store the positions of the center of base of cylinders in a matrix, so each element must be a subarry of 3 elements corresponding to the x, y and z coordinates.

I have already created 3 different matrices for these 3 cordinates but I think that array of array would be more straightforward to work work with.

Thanks.

Subject: Array of array

From: Bruno Luong

Date: 28 Dec, 2012 18:50:08

Message: 10 of 15

"CHINEDU" wrote in message <kbkn0j$1ko$1@newscl01ah.mathworks.com>...

> dpb, I know I can initialize a matrix using A = zeros(20,30)
> how do I initialize a matrix of subarrays?

A = zeros(3,20,30);

Bruno

Subject: Array of array

From: CHINEDU

Date: 29 Dec, 2012 12:17:09

Message: 11 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbkpl0$b33$1@newscl01ah.mathworks.com>...
> "CHINEDU" wrote in message <kbkn0j$1ko$1@newscl01ah.mathworks.com>...
>
> > dpb, I know I can initialize a matrix using A = zeros(20,30)
> > how do I initialize a matrix of subarrays?
>
> A = zeros(3,20,30);
>
> Bruno


Bruno, zeros(3,20,30) creates a 3D array. I want to initialize a 2D array(matrix) where each element would be of the form {1,2,-1}.
e.g for if A is 3-by-2 matrix with subarrays as
A = [(1,2,2) (1 2 3)
       (4 5 6) (2,2,3)
       (0,3,2) (1,2,2)]

ho do I initialize A?

Thanks

Subject: Array of array

From: Bruno Luong

Date: 29 Dec, 2012 12:25:09

Message: 12 of 15

"CHINEDU" wrote in message <kbmn05$srb$1@newscl01ah.mathworks.com>...

>
> Bruno, zeros(3,20,30) creates a 3D array. I want to initialize a 2D array(matrix) where each element would be of the form {1,2,-1}.

{1,2,-1} is a cell.

We told you to avoid using CELL if you don't want to get into trouble later.

Bruno

Subject: Array of array

From: Bruno Luong

Date: 29 Dec, 2012 13:34:09

Message: 13 of 15

"CHINEDU" wrote in message <kbmn05$srb$1@newscl01ah.mathworks.com>...

> e.g for if A is 3-by-2 matrix with subarrays as
> A = [(1,2,2) (1 2 3)
> (4 5 6) (2,2,3)
> (0,3,2) (1,2,2)]
>
> ho do I initialize A?
>
> Thanks

A=cat(3,[1 2 2; 4 5 6; 0 3 2]',[1 2 3; 2 2 3; 1 2 2]')

A(:,1,2)
A(:,3,1)

% Bruno

Subject: Array of array

From: CHINEDU

Date: 29 Dec, 2012 17:11:11

Message: 14 of 15

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kbmrgh$dhv$1@newscl01ah.mathworks.com>...
> "CHINEDU" wrote in message <kbmn05$srb$1@newscl01ah.mathworks.com>...
>
> > e.g for if A is 3-by-2 matrix with subarrays as
> > A = [(1,2,2) (1 2 3)
> > (4 5 6) (2,2,3)
> > (0,3,2) (1,2,2)]
> >
> > ho do I initialize A?
> >
> > Thanks
>
> A=cat(3,[1 2 2; 4 5 6; 0 3 2]',[1 2 3; 2 2 3; 1 2 2]')
>
> A(:,1,2)
> A(:,3,1)
>
> % Bruno

Thanks Bruno,
I have already continued the project without using array of arrays any more. but for learning sake, it is still good to understand how that works.

Thanks

Subject: Array of array

From: Bruno Luong

Date: 29 Dec, 2012 17:23:08

Message: 15 of 15

"CHINEDU" wrote in message <kbn87f$nuu$1@newscl01ah.mathworks.com>...

> I have already continued the project without using array of arrays any more. but for learning sake, it is still good to understand how that works.

Array of (same-size) arrays is no more no less than multi-dimensional array.

Bruno

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