how to make a sub matrix from specific indices

3 Ansichten (letzte 30 Tage)
Kellie
Kellie am 14 Apr. 2023
Kommentiert: Walter Roberson am 14 Apr. 2023
I have a 1400x1400 matrix (A), from this I want to make a 10x10 submatrix but I want it at the followign indices:
312
323
673
876
1031
1326
1344
1354
1359
1384
is this possible?
Thank you!

Antworten (1)

Walter Roberson
Walter Roberson am 14 Apr. 2023
Bearbeitet: Walter Roberson am 14 Apr. 2023
sizeA = [1400 1400];
indices = [312
323
673
876
1031
1326
1344
1354
1359
1384];
[r, c] = ind2sub(sizeA, indices);
nummat = size(r,1);
mats = cell(nummat,1);
for K = 1 : nummat
mats{K} = A(r(K):r(K)+9, c(K):c(K)+9);
end
Yes, it would be possible to vectorize this a bit, but the code would be notably more difficult to understand.
  2 Kommentare
VBBV
VBBV am 14 Apr. 2023
Bearbeitet: VBBV am 14 Apr. 2023
Both ind2sub & sub2ind are able to produce the 10 x10 matrix format, but which one is correct here is confusing for me,
sizeA = [1400 1400];
A = randn(1400);
indices = [312
323
673
876
1031
1326
1344
1354
1359
1384];
[r c] = ind2sub(sizeA, indices);
nummat = size(r,1);
mats = cell(nummat,1);
for K = 1 : nummat
mats{K} = A(r(K):r(K)+9, c(K):c(K)+9);
end
mats{:}
ans = 10×10
1.2274 0.3295 0.7968 1.4923 -0.7608 0.7907 -0.5803 0.2214 1.2191 -0.2977 0.4435 -1.4633 -0.7854 1.7629 -1.2268 0.8160 -1.0832 -0.9987 -0.4603 0.6937 -0.9401 -0.6759 0.8486 -1.0199 2.0416 -0.3673 -0.3748 0.1962 -0.6763 -2.5935 -0.5716 0.4462 1.7819 -0.1581 1.6739 0.0326 0.4167 0.5241 -1.1618 0.8784 -0.8986 -0.6070 0.0067 1.1046 1.2513 -1.4109 -0.9435 0.8504 1.7319 1.0161 -0.2454 -2.1883 -0.1573 1.0006 -0.7103 0.1212 -0.4622 -0.1738 0.3173 -0.1019 0.7547 -0.5365 -0.9112 -2.0777 0.3595 -0.6276 -0.2749 -0.3126 1.2341 0.4672 0.1448 -0.5764 -0.2750 0.2195 1.0517 0.3727 -0.3154 0.7365 0.2040 0.1246 0.5426 -0.1619 0.0019 2.6920 1.1146 0.1145 -0.0051 0.9233 0.4249 1.4796 -0.6364 0.6818 -1.7166 -1.4217 -1.6261 1.8180 -1.3637 0.8519 -0.3774 -0.7804
ans = 10×10
0.6697 -0.2216 0.6877 -1.8931 -0.0014 -0.6697 0.0405 -0.1257 -1.5222 -0.4424 -0.4472 -0.6073 0.4856 -0.2113 -0.0960 0.9896 0.3959 0.1211 -0.7276 0.2936 -0.2850 0.6803 0.3817 1.4337 -1.0523 -0.0551 1.5178 0.9039 1.3457 0.9360 -0.8927 -0.5396 -0.3796 -0.7903 -0.7854 -1.6147 0.3620 0.6523 -1.1203 0.2483 -0.7449 -0.9152 -0.7908 -0.0733 0.5327 -1.7781 1.4260 -0.2516 -1.0976 0.4227 -1.2642 0.1351 0.0347 0.0540 -0.2795 0.6524 -1.9098 1.3335 -1.2926 -0.9181 0.9319 -1.0401 -0.7477 -1.7887 -1.5969 0.5209 0.4045 1.5619 -0.1162 1.7773 0.0869 -0.5220 1.1894 -1.3465 1.5531 0.0609 0.7463 1.0551 -0.0238 -0.1360 -1.2583 0.6302 0.2016 0.3634 0.2360 -0.6430 0.9249 0.7618 0.0457 -0.7809 1.3842 0.3534 -0.0488 1.4713 -0.4480 -1.7879 0.3722 0.8602 -0.3932 0.2565
ans = 10×10
-2.4064 0.5570 -0.8339 -2.4067 1.3686 0.6052 1.7021 -0.0917 0.7401 -0.5899 1.7309 0.0620 1.1621 -0.6396 0.0083 -0.0413 -1.4599 1.4072 -0.9151 -0.0624 -2.1788 -0.1313 0.4904 0.4537 -0.8098 1.0716 -0.6214 -1.2674 1.1373 0.3682 2.3037 0.1686 0.6020 0.7145 -0.5152 -1.0540 0.6063 0.1481 0.6786 -0.7383 1.0603 1.0721 0.5414 -0.2556 -1.2218 -0.2641 -1.7006 -0.9535 1.1841 1.3660 1.1520 0.7860 -0.9208 -0.3916 -0.1032 0.3456 2.0979 -0.1338 -0.5450 -0.3687 -1.6381 0.6963 -1.3700 1.1938 0.3811 -0.5439 1.5586 0.3399 -0.3875 -0.5935 -0.4899 -1.1105 -0.7849 0.5704 0.8833 0.9133 0.8279 0.8836 0.6648 2.0661 0.8146 0.1991 -0.1595 -0.6342 0.1025 0.6582 -0.6770 -1.0532 0.3205 -1.6026 -1.1088 -0.1052 0.8062 -0.1685 -0.9004 0.0265 0.0214 -1.6802 -0.6561 -1.5523
ans = 10×10
1.1268 0.0370 1.2579 -0.6739 -0.4390 -0.4109 0.7583 -0.8408 2.2990 -0.8014 0.1990 0.8289 1.8590 -0.8605 -0.3906 1.5655 1.2351 -0.6535 -1.1849 -0.3863 -0.7625 -2.2754 0.7528 0.3255 -0.8675 0.4560 2.4472 0.8346 0.6872 -0.4408 -0.3979 0.8189 -0.6469 -0.2538 -0.7524 -1.1820 0.4280 1.4433 0.2509 -1.2179 -0.5213 -0.9014 -0.8947 0.4506 0.6618 1.4883 0.0988 0.8976 1.0143 -0.3166 1.2459 0.5731 -0.0862 0.0879 -0.1629 0.0623 1.2049 0.1768 -0.0708 -1.4698 -0.7387 -1.1931 0.7336 -0.9635 0.7394 -1.2931 0.5884 1.0487 -1.6886 0.7649 -0.0832 -0.1566 1.1809 0.9180 0.2742 -0.9146 -0.6467 0.1735 1.1813 -0.9604 -0.1030 -1.3454 -0.9303 -0.5204 2.0201 1.6035 -0.4046 0.0663 0.4674 -1.2442 0.2572 -0.5886 1.2753 -0.7095 -1.3927 -1.5557 -0.4019 2.3746 -1.6129 0.8868
ans = 10×10
0.9847 -0.4020 -0.5251 0.1701 -0.6140 -0.6884 0.6534 -0.3032 0.3091 -0.3692 0.9861 -0.4543 1.5570 -0.2683 0.7104 0.3965 0.9716 -0.1630 -0.5840 0.0969 0.7748 -0.6726 1.0529 -2.1886 0.3496 0.2964 -1.0824 0.0658 0.2957 0.8040 -0.3948 0.1086 0.1299 -1.5286 -0.9352 0.8854 -0.2151 -0.9710 -0.0098 -0.3886 -0.9170 -0.4063 -1.2175 0.2642 0.3906 0.5182 0.9190 -0.1160 0.5534 -2.1486 0.0223 -1.6013 0.8343 -0.9097 1.8630 0.2969 0.5699 -0.4728 -1.7668 0.2987 -0.5719 -0.2976 0.1616 -1.2727 0.6519 -0.3377 -0.0481 0.1742 -0.4020 1.5383 1.9145 -0.2494 0.5216 -1.5958 -0.5605 0.1725 0.2519 1.8173 -0.7086 -0.4622 -0.4151 1.3074 -0.9092 -1.5118 -0.0560 -1.3397 2.1519 0.3999 1.3701 -0.5088 0.1468 -0.1830 -0.3500 -1.2348 -0.0941 -0.1012 1.2819 1.4033 -1.1342 -0.0837
ans = 10×10
0.0933 1.5828 0.4261 -0.4075 1.7207 0.3468 0.7990 0.5301 -2.3321 -0.2226 -0.1458 1.0198 -1.3263 0.4327 -0.2883 0.1084 -0.0221 -0.5067 0.7344 -0.3785 0.6271 -0.2777 -0.2144 1.5244 0.0726 -0.9391 -0.5420 1.5376 -2.1879 1.7058 1.2357 2.7229 0.0155 -0.4603 0.5415 -0.1820 0.1462 0.0159 1.7782 -0.6156 -1.6198 -0.6055 -0.3607 0.5433 -0.0195 -1.8119 0.2418 1.4614 -1.6690 0.2379 -1.3812 -1.1528 -0.7278 0.4328 1.0143 1.0823 -0.6154 1.0609 1.2193 -0.1190 0.0039 -0.9788 -0.0482 0.5554 1.0455 0.3008 -0.4074 -0.6076 0.8555 -0.4619 0.5751 0.4971 -0.5637 -1.4416 0.3489 -0.2736 -0.3581 2.1733 -1.0961 -1.3528 1.7314 -1.7163 -0.7950 0.8097 0.2180 -1.3015 1.1040 -0.4973 0.5934 -0.9828 1.0615 1.2497 -1.7331 0.1935 1.8003 0.3016 -0.9228 0.6502 1.1792 1.5103
ans = 10×10
-0.4503 0.8373 -0.8358 1.6776 1.0439 -1.4601 -0.4207 0.6244 -0.5346 0.3198 0.9552 -0.1955 -1.9583 -0.4792 0.0437 -0.0828 -0.5921 1.1775 -0.1669 0.2423 0.5675 -0.8013 1.5307 -1.1743 -0.1577 -0.2642 0.0203 -1.2842 0.9979 -0.4941 -0.4357 0.3852 0.9015 1.1771 0.1913 1.0853 -1.3117 1.2533 0.0945 1.4830 0.1810 -1.7190 -1.4033 -0.5826 -1.1408 -0.9292 -0.5815 -0.1439 -0.3964 0.4006 0.8684 0.7353 -1.3325 1.5264 -1.1969 -0.5514 -1.0830 0.1895 -2.2601 0.8543 1.9686 2.0115 0.2306 -1.0766 1.7776 -0.3819 1.4037 0.2358 0.0671 0.4554 -1.2876 0.4543 -2.1239 -0.4798 1.3068 -0.6482 -0.0127 -1.4443 -0.4105 0.8932 0.4975 -0.5648 1.7621 -0.3012 -0.8388 0.1537 0.3617 1.5525 0.0675 -1.4487 -0.4951 1.9292 1.7286 -0.1753 -1.1269 -0.9260 0.9436 0.5801 -1.1331 0.4536
ans = 10×10
1.1286 -1.3361 -1.4970 0.6643 -0.7034 0.5047 -0.5886 -0.1161 0.4318 1.5564 -0.5444 0.0786 0.1863 -1.0993 -1.7784 0.2960 -0.6002 -0.6993 -0.4482 1.1978 1.1724 -0.6675 0.9070 -1.5070 -0.0034 -0.5917 1.1190 -0.3524 -0.5115 -1.9743 0.6043 -0.7524 0.2611 0.2953 -0.2356 1.1350 2.1098 0.0302 -0.0072 0.1504 0.3063 0.3137 -0.5007 -0.5035 -0.2251 -0.4456 0.1703 0.0302 -2.4383 -0.5960 -0.3456 2.1602 -0.8945 0.6599 0.7097 0.0978 2.0203 -1.0533 0.7168 -0.3909 -0.4985 2.0481 0.8436 0.1718 0.5416 1.7352 0.8161 1.0455 -0.0929 1.2690 0.4820 0.0320 1.0301 -0.4336 0.5284 0.1875 2.2568 -0.4538 -0.1784 0.2202 -1.3458 0.5211 2.6368 0.6873 -1.4840 0.5279 -0.8495 -0.7011 1.2811 1.4801 0.7558 1.1309 0.3442 0.3185 1.8050 2.3447 -0.5398 1.3449 -0.8441 0.9285
ans = 10×10
-0.3456 2.1602 -0.8945 0.6599 0.7097 0.0978 2.0203 -1.0533 0.7168 -0.3909 -0.4985 2.0481 0.8436 0.1718 0.5416 1.7352 0.8161 1.0455 -0.0929 1.2690 0.4820 0.0320 1.0301 -0.4336 0.5284 0.1875 2.2568 -0.4538 -0.1784 0.2202 -1.3458 0.5211 2.6368 0.6873 -1.4840 0.5279 -0.8495 -0.7011 1.2811 1.4801 0.7558 1.1309 0.3442 0.3185 1.8050 2.3447 -0.5398 1.3449 -0.8441 0.9285 -0.3737 -0.7873 1.6613 0.4257 -0.7976 -0.0925 -1.0963 0.4607 2.2001 -0.6749 1.1886 -0.6613 1.1697 0.3777 -0.6765 0.5386 -0.3835 0.0877 -0.4095 1.2290 -0.8211 0.8848 0.7490 -0.5707 0.9414 -1.5440 -0.8125 -0.5192 0.8658 -0.8438 1.6599 0.7769 0.4541 -0.1691 0.7683 -1.9636 0.9507 0.8829 -1.5058 0.1840 -2.1856 0.2031 -0.3591 0.3669 -0.6781 0.5134 -0.5405 -0.4923 -1.0761 0.7079
ans = 10×10
-1.3066 0.1792 -0.3632 -0.2916 1.6796 1.3848 0.9249 -1.1241 0.8276 -0.6299 0.4465 -1.0432 -2.0024 -0.3961 0.2425 -1.3310 0.9709 0.6972 -1.3396 -1.4757 0.7585 0.0243 0.7183 1.3754 -0.7643 0.8189 2.5932 -0.3269 0.4465 0.9266 0.7364 0.4367 0.3985 -0.2891 -1.4749 -2.3235 -0.2126 2.1990 -0.1747 -0.4467 0.0126 0.3232 1.0698 0.5077 -1.4442 2.2230 0.4276 -1.5945 0.1920 -2.7698 0.8809 -0.1422 1.2161 1.8356 1.0401 -1.0258 -0.2862 -0.3394 0.2612 2.0778 -0.0869 -1.2442 -0.4470 -1.1189 -1.0614 -0.6622 0.9346 -1.3191 -0.1541 -0.4887 -1.7913 -0.5962 0.2035 -0.1535 1.2930 0.4868 1.2997 -1.0880 -0.0098 0.4224 0.9853 0.5005 -0.1731 0.9143 -0.8004 -1.7848 -0.9680 -1.8426 0.2232 1.3747 -1.1119 1.4142 -1.0316 -0.4480 -0.2625 0.1601 -1.4253 -1.2456 0.6185 0.7708
Walter Roberson
Walter Roberson am 14 Apr. 2023
Thanks for catching that, should be ind2sub()... I corrected my code.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by