简体   繁体   中英

Variable length row in a 2D matrix in MATLAB

嗨,我需要在Matlab中存储可变长度的行。有人可以指向正确的方向吗?

I realized the link in my comment wasn't necessarily clear enough for this kind of question, so I thought I'd expand it to an answer with an example.

Using a cell array, you can hold any data type in each cell. Less generally, this means it works for holding vectors of different lengths, which is what you're asking for.

A = [0 1 2];
B = [3 4];

#% assigning a variable into a cell array:
C{1} = A;  #% note the curly braces {} instead of ()
C{2} = B;

#% getting a value out of a cell array:
D = C{2}; #% D is a 1x2 matrix of doubles
E = C(2); #% E is a 1x1 cell

As you can see, to access the elements of a cell array, use curly brackets {} instead of the usual parentheses () , both for reading from/writing to the cell 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