Best method to create multi-dimensional data set?
조회 수: 13 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Karim
2023년 1월 3일
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
% have a look at the data of the 2nd set of the third model:
Model_3_Set_2 = MyData{2,3}
추가 답변 (1개)
the cyclist
2023년 1월 3일
편집: the cyclist
2023년 1월 3일
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)
Model{1}
Model{1}(1)
Model{1}{1}
댓글 수: 3
참고 항목
카테고리
Help Center 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!