简体   繁体   中英

which structure I should use for this matlab arrays

I'm starting to develop a project which uses multi-dimensional arrays very often. my arrays are mostly 2 , 3 dimensional or so. As a 2D array sample consider 'A', I may have 2 or more 1D arrays in a cell.

sth like

A=[1, [78,9] [10,65], 9;
   2 ,       3 ,      6;
   7 ,      [9,1] , [91,41,96][10,-1]]

As you saw in 'A(1,2)' there are two 1D arrays. I don't know which structure I should use to achieve such thing. moreover I want to be able to have access to all those 1D arrays.

please share your knowledge with me.

try using cell or struct i would recommend cell .

Eg preinitialize A1 :

A1=cell(3,3)

(that would be a 3x3 cell array/matrix). Then you can adress elements with curly brackets ({}). Eg:

A1{1,1}= 1;
A1(1,1)={1};

both work. You can also define many cells in one line. Eg:

A1(2,:) = {2,3,6};

For the cases with multiarray entries use another cell structure:

B= {[78,9], [10,65]};
A1(1,2) = {B};

and so on. Pay attention to use curly brackets around B (or the coordinate in A1 )! Otherwise he would try to merge the cells from B in A1 but that won't do any good because B is a 1x2 cell and you want to give it as argument to one cell in A1 .

If you want to return the value inside a cell you have to use curly brackets again:

 A1{1,1}

would return 1.

It depends on what you want to do with such a structure. You could use cell arrays for each 1d array entry, and create a matrix of such cell arrays:

a = {1, 2};
b = {-1, 4, 6};
M = [a b];

Alternatively, you can define a sparse 3d array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM