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

I want to save some results as follows:

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 from 0 to 3000, but I will use only about 100-300 of them. Is it possible to use 0 as an identifier, or will I need to use a minimum value of 1? (Numbers make sense, so I would prefer it if I don't need to change them). Can I use numbers as identifiers in structures?

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 indeed a "number zero" element.

In this case, I will create a bunch of empty cell elements [] in empty positions. Does this cause a problem? I guess it is best to assign the last element first to avoid creating a growing cell, or does it have no effect? Is this an effective way to do this?

What will be the most efficient, struct or cell ? (If possible use struct 's, that is).

My main concern is computing efficiency.

Thanks!

+4
source share
3 answers

As an alternative to EitanT answer, it sounds like card containers - exactly what you need. They can work with any type of key, and the value can be struct or cell .

EDIT:

In your case, it 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 
+3
source

View your options:

Indexing into Cell Arrays

MATLAB indices start at 1, not 0. If you want to store your data in cell arrays, in the worst case scenario, you can always use the k + 1 index to index into the cell corresponding to the k-th identifier (k โ‰ฅ 0). In my opinion, using the last element as a โ€œbase caseโ€ is more confusing. So you will have:

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

Access to fields in structures

Field names in structures cannot begin with numbers, but they are allowed to contain them starting with the second character. Therefore, you can build your structure as follows:

 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 the link to a dynamic field to access any field, for example:

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

I canโ€™t say which alternative seems more elegant, but I believe that indexing in a cell should be faster than references to a dynamic field (but you can check this and prove that I'm wrong).

+5
source

I agree with the idea in a comment by @wakjah. If you are concerned about the effectiveness of your program, it is best to change the interpretation of the problem. In my opinion, there is definitely a way that you could priorotize your data. This prioritization may be based on the time you purchased them, or in relation to the raw data they calculate. If you set any priority for them, you can sort them into a structure or cell (a structure can be faster).

So,

 Priority (Your Current Index Meaning) Data 1 0 magic(4) 2 2 magic(5) 3 7 magic(6) 4 2000 1:3 

Then:

 % Initialize Result structure which is different than your Res. Result(300).Data = 0; % 300 the maximum number of data Result(300).idx = 0; % idx or anything that represent the meaning of your current index. % Assigning k = 1; % Priority index Result(k).idx = 0; Result(k).Data = magic(4); k = k + 1; Result(k).idx = 2; Result(k).Data = magic(5); k = k + 1; Result(k).idx = 7; Result(k).Data = magic(6); k = k + 1; ... 
+1
source

Source: https://habr.com/ru/post/1484175/


All Articles