Best method to create multi-dimensional data set?

11 views (last 30 days)
I have the following example data in Matlab.
Model-1: Set-1: [1 2 3]
Set-2: [5 6 7 8 9]
Set-3: []
Set-4: [21 22 23 24 25 26]
Model-2: Set-1: [2.5 43 22 28 71 101]
Set-2: [4 18 12]
Model-3: Set-1:...
.... and so on.
My goal is to iterate over all Models, then loop over the sets in each model and then read the contents of each set. The sets and contents for each model can different, e.g., Model-1 has 4 sets, Model-2 has only 2 sets, etc.
What would the best way to initialize and populate this information in Matlab? I've tried several variations with multi-dimensional arrays but can't get it working. TIA for the guidance, I'm sure this is quite easy to accomplish.
Joe

Accepted Answer

Karim
Karim on 3 Jan 2023
You could store this in a cell array. See below for a demonstration and some comments on the concept:
num_models = 10;
num_sets = [6 3 7 5 3 2 1 4 6 3]; % each value represents the number of sets for a given model
% create a cell array in which each column is a model and each row is a set
MyData = cell( max(num_sets), num_models);
% populate the cell array with data
for i = 1:num_models
for j = 1:num_sets(i)
% fill with random data
num_rows = randi([1 10]); % pick random number of rows between 1 and 10
num_cols = randi([1 10]); % pick random number of columns between 1 and 10
MyData{i,j} = rand(num_rows,num_cols);
end
end
% have a look at the data
MyData
MyData = 10×10 cell array
{10×2 double} { 5×5 double} { 9×9 double} {7×7 double} {[0.9761 0.1392 0.0064 0.6623 0.9956 0.2634 0.6677 0.8311]} { 9×6 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 9×9 double} { 8×5 double} { 2×9 double} {0×0 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 2×7 double} { 3×3 double} { 9×5 double} {9×2 double} {9×1 double } {10×8 double} {9×9 double} {0×0 double} {0×0 double} {0×0 double} { 2×10 double} { 8×10 double} { 4×2 double} {5×2 double} {[ 0.5797 0.1913 0.3830]} { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 4×9 double} { 3×2 double} { 5×1 double} {0×0 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 7×2 double} { 3×3 double} { 0×0 double} {0×0 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 2×6 double} { 0×0 double} { 0×0 double} {0×0 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {10×3 double} { 6×9 double} { 2×3 double} {6×9 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {10×5 double} { 9×3 double} {10×2 double} {8×9 double} {6×1 double } { 4×1 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} { 7×7 double} {10×7 double} { 4×2 double} {0×0 double} {0×0 double } { 0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
% have a look at the data of the 2nd set of the third model:
Model_3_Set_2 = MyData{2,3}
Model_3_Set_2 = 2×9
0.0702 0.9117 0.4120 0.1954 0.1926 0.5474 0.7631 0.2901 0.2313 0.9708 0.6431 0.8319 0.9188 0.0228 0.5095 0.4399 0.1118 0.1814
  1 Comment
Joe Rustan
Joe Rustan on 4 Jan 2023
This is crystal-clear now, thanks. I was indeed struggling with the curlies vs. parentheses. Much appreciated!

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 3 Jan 2023
Edited: the cyclist on 3 Jan 2023
One method would be nested cell arrays:
Model = cell(3,1); % Three models
Model{1} = cell(4,1); % Model 1 has four sets
Model{2} = cell(2,1); % Model 2 has two sets
Model{3} = cell(7,1); % Model 2 has seven sets
Model{1}{1} = [1 2 3]; % Contents of Model 1 Set 1
Model{1}{2} = [5 6 7 8 9]; % Contents of Model 1 Set 2
Model{2}{1} = [2.5 43 22 28 71 101]; % Contents of Model 1 Set 1
Note that the use of parentheses versus curly braces can be a bit tricky to under at the beginning. Use parentheses for the cell, and curly for the contents of the cell. See the difference here (and note that some cells have empty arrays, because I have not filled them yet.)
Model(1)
ans = 1×1 cell array
{4×1 cell}
Model{1}
ans = 4×1 cell array
{[ 1 2 3]} {[5 6 7 8 9]} {0×0 double } {0×0 double }
Model{1}(1)
ans = 1×1 cell array
{[1 2 3]}
Model{1}{1}
ans = 1×3
1 2 3
  3 Comments
the cyclist
the cyclist on 3 Jan 2023
Not fully redundant! You added some info that I did not.
Joe Rustan
Joe Rustan on 4 Jan 2023
I've accepted Karim's answer, but yours is just as clear of course. Thanks so much!

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!