Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: 2d-array, rows different lengths

In Matlab, I want to create a two-dimensional array. However, I cannot create a matrix, because the rows are all different lengths.

I am new to Matlab, and I would normally do this in C++ by creating an array of pointers, with each pointer pointing towards its own array.

How should I do this in Matlab? Thanks.

like image 759
Karnivaurus Avatar asked Mar 17 '14 17:03

Karnivaurus


2 Answers

You can use cell arrays, which can contain data of varying types and sizes.

Like this:

data = {[1]; [2,2]; [3,3,3]};

Check out here for more examples.

like image 146
herohuyongtao Avatar answered Sep 30 '22 03:09

herohuyongtao


You could use a cell array:

C = {[1,2,3];
     [1,2,3,4,5];
     [1,2]};

Or pad with NaN or 0 or Inf etc

N = [1, 2, 3,   NaN, NaN;
     1, 2, 3,   4,   5;
     1, 2, NaN, NaN, NaN]

It really depends on what you will be doing with your data next

like image 28
Dan Avatar answered Sep 30 '22 03:09

Dan