Cody

Problem 1896. Index one element in each vector of an array along a given dimension

Solution 2243796

Submitted on 29 Apr 2020 by Asif Newaz
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
for a = 1:30 ndms = ceil(rand(1) * 5) + 1 sz = 1 + ceil(rand(1, ndms) * 10); A = rand(sz); sz1 = sz(1); sz(1) = 1; I = ceil(rand(sz) * sz1); B = reshape(A(I(:)+(0:sz1:numel(A)-1)'), size(I)); p = randperm(ndms); A = permute(A, p); I = permute(I, p); B = permute(B, p); assert(isequal(dimsel(A, I), B)); end

ndms = 2 c = 0.5235 0.0038 0.8890 ndms = 2 c = 0.0415 0.0597 0.2975 0.4070 0.9294 0.7003 0.3208 0.7123 0.2069 0.5014 0.2866 ndms = 3 c(:,:,1) = 0.4120 0.8459 0.8516 0.8957 c(:,:,2) = 0.5110 0.7528 0.8497 0.9695 c(:,:,3) = 0.3845 0.9153 0.4609 0.4021 c(:,:,4) = 0.0052 0.2968 0.6755 0.6028 c(:,:,5) = 0.9882 0.4890 0.5820 0.4660 ndms = 5 c(:,:,1,1,1) = 0.2415 0.6627 0.0562 c(:,:,2,1,1) = 0.3924 0.2133 0.4170 c(:,:,1,2,1) = 0.2828 0.0032 0.1664 c(:,:,2,2,1) = 0.0627 0.0418 0.5492 c(:,:,1,1,2) = 0.7906 0.0385 0.0833 c(:,:,2,1,2) = 0.2739 0.0182 0.5465 c(:,:,1,2,2) = 0.3120 0.3242 0.5364 c(:,:,2,2,2) = 0.6667 0.4250 0.4834 c(:,:,1,1,3) = 0.4792 0.3250 0.4718 c(:,:,2,1,3) = 0.5053 0.7077 0.1230 c(:,:,1,2,3) = 0.2507 0.0497 0.1779 c(:,:,2,2,3) = 0.0140 0.2109 0.9227 c(:,:,1,1,4) = 0.8209 0.8058 0.3299 c(:,:,2,1,4) = 0.4311 0.0049 0.6073 c(:,:,1,2,4) = 0.2708 0.8636 0.2944 c(:,:,2,2,4) = 0.2330 0.1451 0.2735 c(:,:,1,1,5) = 0.3489 0.0269 0.7733 c(:,:,2,1,5) = 0.0621 0.3329 0.0821 c(:,:,1,2,5) = 0.6044 0.2942 0.3562 c(:,:,2,2,5) = 0.6050 0.4514 0.5226 c(:,:,1,1,6) = 0.0807 0.5469 0.0488 c(:,:,2,1,6) = 0.9794 0.2202 0.5344 c(:,:,1,2,6) = 0.4978 0.0171 0.1260 c(:,:,2,2,6) = 0.2309 0.3437 0.4258 c(:,:,1,1,7) = 0.1366 0.8786 0.9818 c(:,:,2,1,7) = 0.8800 0.3925 0.6472 c(:,:,1,2,7) = 0.8343 0.5828 0.4939 c(:,:,2,2,7) = 0.3473 0.5993 0.5236 ndms = 2 c = 0.9855 0.3934 0.3027 0.6493 0.6203 0.5416 0.7934 0.2766 ndms = 5 c(:,:,1,1,1) = 0.7953 0.5502 0.5592 0.5572 0.7940 0.3438 0.9744 0.8743 0.9350 0.6956 0.1173 0.8478 0.6244 0.6817 0.8609 0.0859 0.0099 0.0280 0.8610 0.3321 0.0481 0.3958 0.2733 0.8770 0.6402 0.7385 0.8164 c(:,:,2,1,1) = 0.6095 0.1749 0.7852 0.8662 0.9450 0.7021 0.7883 0.0345 0.7303 0.2242 0.1070 0.8706 0.5976 0.2837 0.3968 0.3527 0.5621 0.9213 0.5263 0.7097 0.0648 0.9984 0.6941 0.6690 0.2915 0.9891 0.4589 c(:,:,3,1,1) = 0.1547 0.6188 0.3269 0.7646 0.6714 0.8584 0.2520 0.2148 0.8319 0.5328 0.9422 0.2304 0.9191 0.7100 0.1807 0.4423 0.9294 0.8767 0.4790 0.5413 0.1516 0.1672 0.5930 0.4994 0.7102 0.7634 0.1392 c(:,:,4,1,1) = 0.1790 0.1118 0.3088 0.1336 0.4167 0.4404 0.1567 0.1389 0.5469 0.7898 0.1218 0.4929 0.1170 0.3352 0.9991 0.8049 0.4209 0.3943 0.8215 0.2391 0.4989 0.8307 0.5069 0.5807 0.7462 0.4365 0.4749 c(:,:,5,1,1) = 0.9021 0.3225 0.9396 0.5440 0.8349 0.0272 0.0429 0.7727 0.0353 0.7455 0.3970 0.9427 0.0963 0.8262 0.6948 0.9143 0.0699 0.3774 0.3502 0.6068 0.8809 0.8009 0.0264 0.0641 0.9712 0.4852 0.8827 c(:,:,6,1,1) = 0.8029 0.1078 0.7380 0.9901 0.2369 0.7811 0.0219 0.2177 0.5161 0.6924 0.7596 0.8977 0.4391 0.1383 0.7650 0.2277 0.9052 0.7518 0.8737 0.0494 0.0976 0.1720 0.8348 0.9886 0.6507 0.4533 0.8432 c(:,:,7,1,1) = 0.6042 0.6651 0.9995 0.9754 0.7658 0.7153 0.6332 0.8187 0.7902 0.7488 0.1060 0.6149 0.2380 0.9553 0.9756 0.7933 0.5796 0.5036 0.4353 0.8579 0.9552 0.3567 0.0819 0.1553 0.8751 0.7244 0.1165 c(:,:,1,1,2) = 0.6199 0.3842 0.9714 0.3090 0.7199 0.7064 0.8905 0.2224 0.7075 0.8900 0.5933 0.4492 0.9658 0.0044 0.6394 0.7413 0.9923 0.1855 0.6715 0.1626 0.6321 0.0443 0.4968 0.4988 0.2107 0.6096 0.9440 c(:,:,2,1,2) = 0.2357 0.8520 0.2273 0.3565 0.3093 0.2899 0.4824 0.0546 0.4319 0.5898 0.2457 0.9295 0.5278 0.3990 0.9368 0.2419 0.5834 0.9673 0.4471 0.3760 0.1654 0.6101 0.5612 0.1602 0.8976 0.1970 0.8184 c(:,:,3,1,2) = 0.9152 0.3977 0.1932 0.7602 0.1576 0.2520 0.1451 0.8048 0.1652 0.9213 0.9791 0.2005 0.5624 0.8149 0.1361 0.3380 0.9091 0.2809 0.2857 0.6878 0.1163 0.9822 0.9650 0.1450 0.2280 0.6509 0.0012 c(:,:,4,1,2) = 0.0291 0.5897 0.8419 0.6250 0.5350 0.3438 0.0859 0.9603 0.7839 0.5295 0.1548 0.2664 0.2993 0.9474 0.1005 0.8762 0.8450 0.9815 0.9981 0.0250 0.4994 0.7665 0.2451 0.0332 0.2359 0.9543 0.8995 c(:,:,5,1,2) = 0.7699 0.0487 0.5264 0.7271 0.4092 0.9062 0.3171 0.8633 0.9056 0.9862 0.3412 0.8205 0.1642 0.8412 0.5408 0.5132 0.7526 0.1090 0.2763 0.4239 0.1220 0.0322 0.6020 0.7953 0.1205 0.0718 0.9757 c(:,:,6,1,2) = 0.6536 0.3262 0.1969 0.0861 0.8094 0.6253 0.2313 0.8740 0.1485 0.6580 0.4145 0.9502 0.6836 0.9309 0.8940 0.1087 0.9427 0.5730 0.5767 0.9785 0.9487 0.9377 0.8746 0.9576 0.5340 0.6703 0.8274 c(:,:,7,1,2) = 0.5074 0.9172 0.9536 0.0399 0.9939 0.3259 0.9798 0.6846 0.6051 0.8947 0.0282 0.1007 0.8644 0.2495 0.1830 0.3424 0.6475 0.4023 0.9021 0.0145 0.9068 0.2053 0.7713 0.6395 0.7297 0.9653 0.5643 c(:,:,1,1,3) = 0.7310 0.8122 0.6309 0.3173 0.2789 0.0840 0.6498 0.8742 0.8230 0.6809 0.4857 0.8815 0.7435 0.0016 0.8156 0.5628 0.7095 0.3662 0.6943 0.3802 0.2178 0.4064 0.5718 0.4927 0.0405 0.1139 0.8271 c(:,:,2,1,3) = 0.0714 0.4708 0.1672 0.1607 0.0227 0.4717 0.0239 0.0195 0.5247 0.3298 0.5835 0.3383 0.5790 0.9142 0.5483 0.7675 0.9428 0.3576 0.8337 0.0282 0.6817 0.1301 0.8145 0.3848 0.4614 0.5624 0.1180 c(:,:,3,1,3) = 0.8394 0.4101 0.5111 0.1230 0.4837 0.6309 0.3338 0.3878 0.9934 0.7275 0.3488 0.2025 0.4969 0.1266 0.6338 0.7590 0.1997 0.9168 0.5164 0.9664 0.6732 0.1248 0.0826 0.5885 0.9374 0.6874 0.9816 c(:,:,4,1,3) = 0.8113 0.4921 0.1321 0.4788 0.0410 0.7151 0.9886 0.7370 0.8461 0.2994 0.2999 0.6933 0.7411 0.3294 0.1844 0.6380 0.8900 0.5070 0.7375 0.7028 0.1921 0.3210 0.4188 0.7584 0.5333 0.5779 0.8164 c(:,:,5,1,3) = 0.3287 0.3271 0.3395 0.7370 0.0662 0.1600 0.4126 0.9658 0.0085 0.5207 0.4559 0.2194 0.1332 0.8954 0.4838 0.6221 0.5701 0.5437 0.5580 0.6339 0.8963 0.8742 0.4619 0.8341 0.5569 0.6029 0.3735 c(:,:,6,1,3) = 0.1384 0.2241 0.2253 0.8837 0.6275 0.5008 0.9917 0.8812 0.8932 0.8198 0.1170 0.9605 0.1943 0.3001 0.5903 0.7349 0.8346 0.8633 0.8843 0.4826 0.9458 0.8253 0.3087 0.0393 0.8021 0.2249 0.5846 c(:,:,7,1,3) = 0.3445 0.8867 0.7155 0.0707 0.4723 0.4471 0.8363 0.5877 0.1423 0.4488 0.0683 0.3755 0.5614 0.7832 0.3207 0.4167 0.7991 0.8030 0.0284 0.1614 0.4932 0.7018 0.9107 0.7257 0.9464 0.8059 0.0654 c(:,:,1,1,4) = 0.4781 0.8020 0.4505 0.9792 0.5616 0.1150 0.2669 0.0577 0.2805 0.7572 0.8528 0.7311 0.1897 0.1955 0.8498 0.2907 0.4956 0.0613 0.5046 0.3780 0.5373 0.8887 0.0204 0.7705 0.3683 0.0820 0.5324 c(:,:,2,1,4) = 0.3912 0.2038 0.5669 0.9067 0.4741 0.4484 0.5979 0.2863 0.8246 0.8527 0.1804 0.3568 0.7325 0.3097 0.3134 0.6961 0.1923 0.3037 0.4293 0.5090 0.3253 0.3735 0.3572 0.0207 0.0105 0.0831 0.2766 c(:,:,3,1,4) = 0.0341 0.6558 0.9335 0.1076 0.7141 0.3358 0.4070 0.2201 0.6471 0.5236 0.7566 0.0623 0.6586 0.6437 0.1733 0.4144 0.2724 0.2823 0.3694 0.3214 0.6792 0.7999 0.6763 0.9584 0.3423 0.0097 0.6678 c(:,:,4,1,4) = 0.1429 0.0337 0.1311 0.2158 0.4568 0.9328 0.2459 0.2695 0.8120 0.9631 0.4943 0.0192 0.3927 0.3777 0.2869 0.9974 0.0323 0.1892 0.3025 0.6374 0.5307 0.3419 0.3052 0.6200 0.0771 0.8296 0.1507 c(:,:,5,1,4) = 0.6094 0.4314 0.6876 0.7230 0.0188 0.8715 0.0410 0.3683 0.3028 0.5457 0.1594 0.4350 0.0786 0.4492 0.8541 0.1856 0.3962 0.5781 0.9073 0.9644 0.5867 0.7190 0.8786 0.9857 0.3854 0.5431 0.6754 c(:,:,6,1,4) = 0.0306 0.9331 0.3458 0.3803 0.8903 0.3165 0.3141 0.2560 0.8355 0.1141 0.0544 0.9977 0.1524 0.7375 0.4340 0.9558 0.2403 0.6591 0.6556 ...

Community Treasure Hunt

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

Start Hunting!