Arrays 从矩阵创建结构数组
我有一个类似以下结构的数组:Arrays 从矩阵创建结构数组,arrays,matlab,matrix,Arrays,Matlab,Matrix,我有一个类似以下结构的数组: AAA(1).Size = rand(1,10); AAA(2).Size = rand(1,10); AAA(...).Size = rand(1,10); ... 我有这样一个矩阵: mMatrix = [rand(1,100); rand(1,100); ...] 如何在一次调用中将mMatrix的行值分配给AAA(此处称为NumberOfElement)中的新结构,而不必使用循环 nRow = size(mMatrix, 1); for ii = 1:n
AAA(1).Size = rand(1,10);
AAA(2).Size = rand(1,10);
AAA(...).Size = rand(1,10);
...
我有这样一个矩阵:
mMatrix = [rand(1,100); rand(1,100); ...]
如何在一次调用中将mMatrix
的行值分配给AAA
(此处称为NumberOfElement
)中的新结构,而不必使用循环
nRow = size(mMatrix, 1);
for ii = 1:nRow
AAA(ii).NumberOfElement = mMatrix(ii,:);
end
此外,是否可以对AAA
中的多个矩阵和多个新结构执行此操作,例如:
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
您可以将矩阵
mMatrix
拆分为多个单元格,每个单元格都是矩阵的一行。我们可以这样做,然后通过一个展开调用分配给结构中的每个元素。无论之前是否声明了结构,这都将起作用。因此:
c = mat2cell(mMatrix, ones(size(mMatrix,1),1), size(mMatrix, 2));
[AAA(1:size(mMatrix,1)).NumberOfElement] = c{:};
示例使用 我们可以使用
for
循环遍历每个结构,以显示每个NumberOfElement
字段的外观:
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
上面的代码显示了存储在mMatrix
中的矩阵的每一行,以及结构中每个元素的实际字段
我得到:
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
您可以将矩阵
mMatrix
拆分为多个单元格,每个单元格都是矩阵的一行。我们可以这样做,然后通过一个展开调用分配给结构中的每个元素。无论之前是否声明了结构,这都将起作用。因此:
c = mat2cell(mMatrix, ones(size(mMatrix,1),1), size(mMatrix, 2));
[AAA(1:size(mMatrix,1)).NumberOfElement] = c{:};
示例使用 我们可以使用
for
循环遍历每个结构,以显示每个NumberOfElement
字段的外观:
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
上面的代码显示了存储在mMatrix
中的矩阵的每一行,以及结构中每个元素的实际字段
我得到:
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
这似乎适用于
多个矩阵和多个新结构的一般情况-
输入和输出样本-
>> 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
其他矩阵也是如此。这似乎适用于多个矩阵和多个新结构的一般情况-
输入和输出样本-
>> 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
依此类推其他矩阵。使用您的示例,我只得到1个字段。我无法访问AAA(2).NumberOfElement.@m_power-开始时我说假设您的结构已经定义,所以您需要执行我在示例中所做的操作。同样地,创建一个空的结构,然后使用repmat
在矩阵中复制任意多的行,然后运行代码。@m_power-我编辑了我的答案以澄清这一点。我复制并执行了您的代码(即示例使用),它初始化AAA。我将[AAA.NumberOfElement]=c{:}
替换为[AAA(1:4).NumberOfElement]=c{:}
,现在它可以工作了。但我不明白你是如何在你发布的示例中得到带有字段NumberOfElement的4x1结构数组的。我投了赞成票,因为它清楚地回答了这个问题,但老实说,我认为使用循环比这种方法更好。使用你的示例,我只得到1个字段。我无法访问AAA(2).NumberOfElement.@m_power-开始时我说假设您的结构已经定义,所以您需要执行我在示例中所做的操作。同样地,创建一个空的结构,然后使用repmat
在矩阵中复制任意多的行,然后运行代码。@m_power-我编辑了我的答案以澄清这一点。我复制并执行了您的代码(即示例使用),它初始化AAA。我将[AAA.NumberOfElement]=c{:}
替换为[AAA(1:4).NumberOfElement]=c{:}
,现在它可以工作了。但我不知道你是如何在你发布的示例中得到带有字段NumberOfElement的4x1结构数组的。我投了赞成票,因为它清楚地回答了这个问题,但老实说,我认为使用循环比使用这种方法更好。只是一个旁注:可能有理由避免循环,但是,如果您只需要容易理解和执行的代码,就应该认真考虑这种结构的循环。你应该认真考虑这类任务的一个循环:结构。@ D.NNISJAHURUDIN -同意。我赞成,因为它清楚地回答了这个问题,但老实说,我认为使用循环比这个方法更好。特别处理细胞可能会使它变慢@DennisJaheruddin和Divakar——双方都同意。Donald Knuth总是说过早优化是万恶之源。如果您可以量化地衡量性能,并且如果您知道取消for
循环肯定会更快,那么就这样做。对于
,我当然会选择,因为它也更具可读性。我确实质疑在没有循环的情况下这样做,特别是因为我们需要将它们转换为单元格。我投了赞成票,因为它清楚地回答了这个问题,但老实说,我认为使用循环会比这种方法更好。@DennisJaheruddin同意这一点!特别处理细胞可能会使它变慢@DennisJaheruddin和Divakar——双方都同意。Donald Knuth总是说过早优化是万恶之源。如果您可以量化地衡量性能,并且如果您知道取消for
循环肯定会更快,那么就这样做。对于
,我当然会选择,因为它也更具可读性。我确实质疑在没有循环的情况下这样做,特别是因为我们需要将这些转化为细胞。