m_power
m_power

Reputation: 3204

Creating array of structure from matrix

I have an array of structure similar to this:

AAA(1).Size = rand(1,10);
AAA(2).Size = rand(1,10);
AAA(...).Size = rand(1,10);
...

I have a matrix such as this:

mMatrix = [rand(1,100); rand(1,100); ...]

How can I assign the row values of mMatrix to a new structure in AAA (here called NumberOfElement), in a single call, without having to use a loop?

nRow = size(mMatrix, 1);
for ii = 1:nRow
    AAA(ii).NumberOfElement = mMatrix(ii,:);
end

Also, could this be done for multiple matrices and multiple new structures in AAA, such as:

nRow = size(mMatrixDataset1, 1); % The matrices have the same number of rows
for ii = 1:nRow
    AAA(ii).NumberOfElementDataset1 = mMatrixDataset1(ii,:);
    AAA(ii).NumberOfElementDataset2 = mMatrixDataset2(ii,:);
    AAA(ii).NumberOfElementDataset3 = mMatrixDataset3(ii,:);
    ...
end

Upvotes: 1

Views: 61

Answers (2)

rayryeng
rayryeng

Reputation: 104503

You can split up the matrix mMatrix into multiple cells where each cell would be a row of your matrix. We can do this with mat2cell and then assign to each element in your structure with a single unfolding call. This will work regardless of whether the structure has been declared previously or not. Therefore:

c = mat2cell(mMatrix, ones(size(mMatrix,1),1), size(mMatrix, 2));
[AAA(1:size(mMatrix,1)).NumberOfElement] = c{:};

Example use

%// Set up code
mMatrix = rand(4, 100);

%// Code I wrote above
c = mat2cell(mMatrix, ones(size(mMatrix,1),1), size(mMatrix, 2));
[AAA(1:size(mMatrix,1)).NumberOfElement] = c{:};

AAA = 

1x4 struct array with fields:

    NumberOfElement

We can go through each structure with a for loop to show what each NumberOfElement field looks like:

for ii = 1 : size(mMatrix, 1)
    disp(['Structure ' num2str(ii)]);
    disp(['Row ' num2str(ii) ' of matrix']);
    disp(mMatrix(ii,:));
    disp('Structure contents')
    disp(AAA(ii).NumberOfElement);
end

The above code shows you each row of the matrix stored in mMatrix and what the actual field is for each element in your structure.

I get:

Structure 1
Row 1 of matrix
  Columns 1 through 8
    0.4229    0.6959    0.0688    0.4076    0.5313    0.7788    0.1537    0.4574
  Columns 9 through 16
    0.6377    0.2891    0.2548    0.3445    0.6022    0.4624    0.3225    0.1759
  Columns 17 through 24
    0.3411    0.2428    0.1887    0.6834    0.6476    0.2089    0.6073    0.7703
  Columns 25 through 32
    0.8419    0.5822    0.3181    0.4795    0.5439    0.2187    0.4046    0.6279
  Columns 33 through 40
    0.1920    0.5254    0.3935    0.3477    0.0445    0.6878    0.6834    0.3309
  Columns 41 through 48
    0.8217    0.7691    0.3774    0.3276    0.7689    0.5144    0.1999    0.7900
  Columns 49 through 56
    0.1117    0.1897    0.8507    0.5828    0.0005    0.5277    0.4981    0.7386
  Columns 57 through 64
    0.0835    0.8908    0.9283    0.8627    0.5523    0.3624    0.1231    0.0427
  Columns 65 through 72
    0.6952    0.1239    0.2703    0.4170    0.1057    0.5737    0.7378    0.9844
  Columns 73 through 80
    0.1776    0.9391    0.4671    0.5590    0.0542    0.8985    0.7069    0.4648
  Columns 81 through 88
    0.1781    0.3358    0.6754    0.7455    0.5972    0.8949    0.4417    0.0934
  Columns 89 through 96
    0.9954    0.2982    0.6311    0.9051    0.3381    0.0484    0.7297    0.6925
  Columns 97 through 100
    0.7802    0.1048    0.8905    0.0729
Structure contents
  Columns 1 through 8
    0.4229    0.6959    0.0688    0.4076    0.5313    0.7788    0.1537    0.4574
  Columns 9 through 16
    0.6377    0.2891    0.2548    0.3445    0.6022    0.4624    0.3225    0.1759
  Columns 17 through 24
    0.3411    0.2428    0.1887    0.6834    0.6476    0.2089    0.6073    0.7703
  Columns 25 through 32
    0.8419    0.5822    0.3181    0.4795    0.5439    0.2187    0.4046    0.6279
  Columns 33 through 40
    0.1920    0.5254    0.3935    0.3477    0.0445    0.6878    0.6834    0.3309
  Columns 41 through 48
    0.8217    0.7691    0.3774    0.3276    0.7689    0.5144    0.1999    0.7900
  Columns 49 through 56
    0.1117    0.1897    0.8507    0.5828    0.0005    0.5277    0.4981    0.7386
  Columns 57 through 64
    0.0835    0.8908    0.9283    0.8627    0.5523    0.3624    0.1231    0.0427
  Columns 65 through 72
    0.6952    0.1239    0.2703    0.4170    0.1057    0.5737    0.7378    0.9844
  Columns 73 through 80
    0.1776    0.9391    0.4671    0.5590    0.0542    0.8985    0.7069    0.4648
  Columns 81 through 88
    0.1781    0.3358    0.6754    0.7455    0.5972    0.8949    0.4417    0.0934
  Columns 89 through 96
    0.9954    0.2982    0.6311    0.9051    0.3381    0.0484    0.7297    0.6925
  Columns 97 through 100
    0.7802    0.1048    0.8905    0.0729
Structure 2
Row 2 of matrix
  Columns 1 through 8
    0.0942    0.6999    0.3196    0.8200    0.3251    0.4235    0.2810    0.8754
  Columns 9 through 16
    0.9577    0.6718    0.2240    0.7805    0.3868    0.4243    0.7847    0.7218
  Columns 17 through 24
    0.6074    0.9174    0.2875    0.5466    0.6790    0.7093    0.4501    0.3502
  Columns 25 through 32
    0.8329    0.5407    0.1192    0.6393    0.7210    0.1058    0.4484    0.7720
  Columns 33 through 40
    0.1389    0.5303    0.6714    0.1500    0.7549    0.3592    0.7040    0.4243
  Columns 41 through 48
    0.4299    0.3968    0.2160    0.6713    0.1673    0.8843    0.4070    0.3185
  Columns 49 through 56
    0.1363    0.4950    0.5606    0.8154    0.8654    0.4795    0.9009    0.5860
  Columns 57 through 64
    0.6260    0.9823    0.5801    0.4843    0.6299    0.0495    0.2055    0.6352
  Columns 65 through 72
    0.4991    0.4904    0.2085    0.2060    0.1420    0.0521    0.0634    0.8589
  Columns 73 through 80
    0.3986    0.3013    0.6482    0.8541    0.1771    0.1182    0.9995    0.7640
  Columns 81 through 88
    0.3596    0.1757    0.4685    0.7363    0.2999    0.0715    0.0133    0.3074
  Columns 89 through 96
    0.3321    0.0464    0.0899    0.5338    0.2940    0.6679    0.7073    0.5567
  Columns 97 through 100
    0.3376    0.1279    0.7990    0.0885
Structure contents
  Columns 1 through 8
    0.0942    0.6999    0.3196    0.8200    0.3251    0.4235    0.2810    0.8754
  Columns 9 through 16
    0.9577    0.6718    0.2240    0.7805    0.3868    0.4243    0.7847    0.7218
  Columns 17 through 24
    0.6074    0.9174    0.2875    0.5466    0.6790    0.7093    0.4501    0.3502
  Columns 25 through 32
    0.8329    0.5407    0.1192    0.6393    0.7210    0.1058    0.4484    0.7720
  Columns 33 through 40
    0.1389    0.5303    0.6714    0.1500    0.7549    0.3592    0.7040    0.4243
  Columns 41 through 48
    0.4299    0.3968    0.2160    0.6713    0.1673    0.8843    0.4070    0.3185
  Columns 49 through 56
    0.1363    0.4950    0.5606    0.8154    0.8654    0.4795    0.9009    0.5860
  Columns 57 through 64
    0.6260    0.9823    0.5801    0.4843    0.6299    0.0495    0.2055    0.6352
  Columns 65 through 72
    0.4991    0.4904    0.2085    0.2060    0.1420    0.0521    0.0634    0.8589
  Columns 73 through 80
    0.3986    0.3013    0.6482    0.8541    0.1771    0.1182    0.9995    0.7640
  Columns 81 through 88
    0.3596    0.1757    0.4685    0.7363    0.2999    0.0715    0.0133    0.3074
  Columns 89 through 96
    0.3321    0.0464    0.0899    0.5338    0.2940    0.6679    0.7073    0.5567
  Columns 97 through 100
    0.3376    0.1279    0.7990    0.0885
Structure 3
Row 3 of matrix
  Columns 1 through 8
    0.5985    0.6385    0.5309    0.7184    0.1056    0.0908    0.4401    0.5181
  Columns 9 through 16
    0.2407    0.6951    0.6678    0.6753    0.9160    0.4609    0.4714    0.4735
  Columns 17 through 24
    0.1917    0.2691    0.0911    0.4257    0.6358    0.2362    0.4587    0.6620
  Columns 25 through 32
    0.2564    0.8699    0.9398    0.5447    0.5225    0.1097    0.3658    0.9329
  Columns 33 through 40
    0.6963    0.8611    0.7413    0.5861    0.2428    0.7363    0.4423    0.2703
  Columns 41 through 48
    0.8878    0.8085    0.7904    0.4386    0.8620    0.5880    0.7487    0.5341
  Columns 49 through 56
    0.6787    0.1476    0.9296    0.8790    0.6126    0.8013    0.5747    0.2467
  Columns 57 through 64
    0.6609    0.7690    0.0170    0.8449    0.0320    0.4896    0.1465    0.2819
  Columns 65 through 72
    0.5358    0.8530    0.5650    0.9479    0.1665    0.9312    0.8604    0.7856
  Columns 73 through 80
    0.1339    0.2955    0.0252    0.3479    0.6628    0.9884    0.2878    0.8182
  Columns 81 through 88
    0.0567    0.2089    0.9121    0.5619    0.1341    0.2425    0.8972    0.4561
  Columns 89 through 96
    0.2973    0.5054    0.0809    0.1092    0.7463    0.6035    0.7814    0.3965
  Columns 97 through 100
    0.6079    0.5495    0.7343    0.7984
Structure contents
  Columns 1 through 8
    0.5985    0.6385    0.5309    0.7184    0.1056    0.0908    0.4401    0.5181
  Columns 9 through 16
    0.2407    0.6951    0.6678    0.6753    0.9160    0.4609    0.4714    0.4735
  Columns 17 through 24
    0.1917    0.2691    0.0911    0.4257    0.6358    0.2362    0.4587    0.6620
  Columns 25 through 32
    0.2564    0.8699    0.9398    0.5447    0.5225    0.1097    0.3658    0.9329
  Columns 33 through 40
    0.6963    0.8611    0.7413    0.5861    0.2428    0.7363    0.4423    0.2703
  Columns 41 through 48
    0.8878    0.8085    0.7904    0.4386    0.8620    0.5880    0.7487    0.5341
  Columns 49 through 56
    0.6787    0.1476    0.9296    0.8790    0.6126    0.8013    0.5747    0.2467
  Columns 57 through 64
    0.6609    0.7690    0.0170    0.8449    0.0320    0.4896    0.1465    0.2819
  Columns 65 through 72
    0.5358    0.8530    0.5650    0.9479    0.1665    0.9312    0.8604    0.7856
  Columns 73 through 80
    0.1339    0.2955    0.0252    0.3479    0.6628    0.9884    0.2878    0.8182
  Columns 81 through 88
    0.0567    0.2089    0.9121    0.5619    0.1341    0.2425    0.8972    0.4561
  Columns 89 through 96
    0.2973    0.5054    0.0809    0.1092    0.7463    0.6035    0.7814    0.3965
  Columns 97 through 100
    0.6079    0.5495    0.7343    0.7984
Structure 4
Row 4 of matrix
  Columns 1 through 8
    0.4709    0.0336    0.6544    0.9686    0.6110    0.2665    0.5271    0.9436
  Columns 9 through 16
    0.6761    0.0680    0.8444    0.0067    0.0012    0.7702    0.0358    0.1527
  Columns 17 through 24
    0.7384    0.7655    0.5762    0.6444    0.9452    0.1194    0.6619    0.4162
  Columns 25 through 32
    0.6135    0.2648    0.6456    0.6473    0.9937    0.0636    0.7635    0.9727
  Columns 33 through 40
    0.0938    0.4849    0.5201    0.2621    0.4424    0.3947    0.0196    0.1971
  Columns 41 through 48
    0.3912    0.7551    0.9493    0.8335    0.9899    0.1548    0.8256    0.0900
  Columns 49 through 56
    0.4952    0.0550    0.6967    0.9889    0.9900    0.2278    0.8452    0.6664
  Columns 57 through 64
    0.7298    0.5814    0.1209    0.2094    0.6147    0.1925    0.1891    0.5386
  Columns 65 through 72
    0.4452    0.8739    0.6403    0.0821    0.6210    0.7287    0.9344    0.5134
  Columns 73 through 80
    0.0309    0.3329    0.8422    0.4460    0.3308    0.5400    0.4145    0.1002
  Columns 81 through 88
    0.5219    0.9052    0.1040    0.1842    0.2126    0.0538    0.1967    0.1017
  Columns 89 through 96
    0.0620    0.7614    0.7772    0.8258    0.0103    0.5261    0.2880    0.0616
  Columns 97 through 100
    0.7413    0.4852    0.0513    0.9430
Structure contents
  Columns 1 through 8
    0.4709    0.0336    0.6544    0.9686    0.6110    0.2665    0.5271    0.9436
  Columns 9 through 16
    0.6761    0.0680    0.8444    0.0067    0.0012    0.7702    0.0358    0.1527
  Columns 17 through 24
    0.7384    0.7655    0.5762    0.6444    0.9452    0.1194    0.6619    0.4162
  Columns 25 through 32
    0.6135    0.2648    0.6456    0.6473    0.9937    0.0636    0.7635    0.9727
  Columns 33 through 40
    0.0938    0.4849    0.5201    0.2621    0.4424    0.3947    0.0196    0.1971
  Columns 41 through 48
    0.3912    0.7551    0.9493    0.8335    0.9899    0.1548    0.8256    0.0900
  Columns 49 through 56
    0.4952    0.0550    0.6967    0.9889    0.9900    0.2278    0.8452    0.6664
  Columns 57 through 64
    0.7298    0.5814    0.1209    0.2094    0.6147    0.1925    0.1891    0.5386
  Columns 65 through 72
    0.4452    0.8739    0.6403    0.0821    0.6210    0.7287    0.9344    0.5134
  Columns 73 through 80
    0.0309    0.3329    0.8422    0.4460    0.3308    0.5400    0.4145    0.1002
  Columns 81 through 88
    0.5219    0.9052    0.1040    0.1842    0.2126    0.0538    0.1967    0.1017
  Columns 89 through 96
    0.0620    0.7614    0.7772    0.8258    0.0103    0.5261    0.2880    0.0616
  Columns 97 through 100
    0.7413    0.4852    0.0513    0.9430

Upvotes: 3

Divakar
Divakar

Reputation: 221564

This seems to work for the general case of multiple matrices and multiple new structures using cell2struct -

num_matrices = 3; %// Number of matrices to be used
fnames = {'set1','set2','set3'} %// Fieldnames for each matrix
M = cat(1,mat1,mat2,mat3) %// Concatenate all matrices into a single one

%// Form cell array version of the concatenated array and then the struct
C = reshape(mat2cell(M,ones(1,size(M,1)),size(M,2)),size(mat1,1),[])

%// Finally create the struct 
AAA = cell2struct(C,fnames,2)

Sample inputs and outputs -

>> mat1
mat1 =
    0.2232    0.9404    0.3981    0.6934    0.5052    0.6516    0.1042
    0.5832    0.8867    0.2036    0.5595    0.2792    0.4844    0.9474
    0.8316    0.0259    0.5718    0.0322    0.4067    0.9513    0.9229
    0.3041    0.2078    0.4503    0.4658    0.2488    0.0533    0.8370
>> AAA.set1
ans =
    0.2232    0.9404    0.3981    0.6934    0.5052    0.6516    0.1042
ans =
    0.5832    0.8867    0.2036    0.5595    0.2792    0.4844    0.9474
ans =
    0.8316    0.0259    0.5718    0.0322    0.4067    0.9513    0.9229
ans =
    0.3041    0.2078    0.4503    0.4658    0.2488    0.0533    0.8370

>> mat2
mat2 =
    0.1468    0.8309    0.3492    0.5820    0.1021    0.4695    0.5848
    0.9706    0.2005    0.5406    0.2961    0.3054    0.7118    0.1053
    0.4054    0.6320    0.3319    0.9196    0.8603    0.2329    0.7693
    0.8479    0.5877    0.8974    0.0810    0.8876    0.0734    0.5933
>> AAA.set2
ans =
    0.1468    0.8309    0.3492    0.5820    0.1021    0.4695    0.5848
ans =
    0.9706    0.2005    0.5406    0.2961    0.3054    0.7118    0.1053
ans =
    0.4054    0.6320    0.3319    0.9196    0.8603    0.2329    0.7693
ans =
    0.8479    0.5877    0.8974    0.0810    0.8876    0.0734    0.5933

and so on for other matrices.

Upvotes: 2

Related Questions