Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using non-continuous integers as identifiers in cells or structs in Matlab

I want to store some results in the following way:

Res.0 = magic(4);      % or Res.baseCase = magic(4);
Res.2 = magic(5);      % I would prefer to use integers on all other
Res.7 = magic(6);      % elements than the first.
Res.2000 = 1:3;

I want to use numbers between 0 and 3000, but I will only use approx 100-300 of them. Is it possible to use 0 as an identifier, or will I have to use a minimum value of 1? (The numbers have meaning, so I would prefer if I don't need to change them). Can I use numbers as identifiers in structs?

I know I can do the following:

Res{(last number + 1)} = magic(4);    
Res{2} = magic(5);
Res{7} = magic(6);
Res{2000} = 1:3;

And just remember that the last element is really the "number zero" element.

In this case I will create a bunch of empty cell elements [] in the non-populated positions. Does this cause a problem? I assume it will be best to assign the last element first, to avoid creating a growing cell, or does this not have an effect? Is this an efficient way of doing this?

Which will be most efficient, struct's or cell's? (If it's possible to use struct's, that is).

My main concern is computational efficiency.

Thanks!

like image 550
Stewie Griffin Avatar asked Jan 14 '23 05:01

Stewie Griffin


2 Answers

Let's review your options:

Indexing into a cell arrays

MATLAB indices start from 1, not from 0. If you want to store your data in cell arrays, in the worst case, you could always use the subscript k + 1 to index into cell corresponding to the k-th identifier (k ≥ 0). In my opinion, using the last element as the "base case" is more confusing. So what you'll have is:

Res{1} = magic(4);                   %// Base case
Res{2} = magic(5);                   %// Corresponds to identifier 1
...
Res{k + 1} = ...                     %// Corresponds to indentifier k

Accessing fields in structures

Field names in structures are not allowed to begin with numbers, but they are allowed to contain them starting from the second character. Hence, you can build your structure like so:

Res.c0 = magic(4);                   %// Base case
Res.c1 = magic(5);                   %// Corresponds to identifier 1
Res.c2 = magic(6);                   %// Corresponds to identifier 2
%// And so on...

You can use dynamic field referencing to access any field, for instance:

k = 3;
kth_field = Res.(sprintf('c%d', k)); %// Access field k = 3 (i.e field 'c3')

I can't say which alternative seems more elegant, but I believe that indexing into a cell should be faster than dynamic field referencing (but you're welcome to check that out and prove me wrong).

like image 68
Eitan T Avatar answered Jan 28 '23 17:01

Eitan T


As an alternative to EitanT's answer, it sounds like matlab's map containers are exactly what you need. They can deal with any type of key and the value may be a struct or cell.

EDIT:

In your case this will be:

k = {0,2,7,2000};
Res = {magic(4),magic(5),magic(6),1:3};
ResMap = containers.Map(k, Res)

ResMap(0)    
ans =
16     2     3    13
 5    11    10     8
 9     7     6    12
 4    14    15     1
like image 30
Marc Claesen Avatar answered Jan 28 '23 16:01

Marc Claesen