Clear Filters
Clear Filters

Subsetting an n-d array with no loop

1 view (last 30 days)
I have an n-d array that I want to subset along specific dimensions accordingly.
For instance, consider the following array,
Consider also a permutation or selection of specific indices from the last dimension:
I want to extract an n-d array B that contains all the diagonal elements of the 1st and last dimensions (applying for that last dimension the σ transformation) :
One option, rather brute-force, is to do the following:
A = rand(10,25,7,10);
sigma = [5,5,4,1,1,9,8,7,2,4];
for i=1:10
B(i,:,:)= A (i,:,:,sigma(i))
end
However, if I deal with very large arrays, this requires a loop which really slows down my code.
I have tried subsetting like below, but Matlab doesn't understand that the index for the 1st dimension runs with the index for last dimension.
C = A(1:10,:,:,sigma(1:10))
If I do this, I end up with a matrix, which would be the following
Is there a way of subsetting an n-d array along specific dimensions simultaneously without a loop ?

Accepted Answer

Vilém Frynta
Vilém Frynta on 12 Apr 2023
Moved: Walter Roberson on 12 Apr 2023
Could this work for you? It may be too "sample specific" instead working for every example.
This script generates a 4D array A of random numbers, and an array sigma of indices (which you provided). It then uses these indices to extract elements from A along the fourth dimension, and saves the results in a new array B, creating a new 3D array that only contains the elements of A selected by the sigma array.
sub2ind function needs same length vectors, so it's been workarounded a bit.
A = rand(10,25,7,10);
sigma = [5,5,4,1,1,9,8,7,2,4];
idx = sub2ind(size(A), repmat((1:10)',25*7,1), ...
repmat(reshape(repmat((1:25)',1,7),[],1),10,1), ...
repmat(reshape(repmat((1:7)',25,1),[],1),10,1), ...
repmat(sigma',25*7,1));
B = reshape(A(idx), [10, 25, 7]);
disp(B)
(:,:,1) = Columns 1 through 19 0.8419 0.1654 0.4302 0.0222 0.1194 0.4118 0.8931 0.6330 0.7192 0.4322 0.2011 0.2726 0.9072 0.1600 0.8224 0.5720 0.2752 0.6638 0.9715 0.1068 0.2722 0.2647 0.3641 0.5543 0.1652 0.7239 0.3740 0.8143 0.3968 0.7299 0.7016 0.2639 0.0332 0.9909 0.0442 0.9355 0.4878 0.3770 0.1411 0.5408 0.4414 0.0513 0.8026 1.0000 0.0751 0.5718 0.8551 0.5188 0.3871 0.0247 0.1508 0.5312 0.5501 0.1578 0.6124 0.7138 0.7429 0.3241 0.2109 0.7512 0.1503 0.8854 0.9613 0.2702 0.2072 0.3297 0.1601 0.5885 0.8163 0.7043 0.2722 0.3613 0.4253 0.2086 0.1753 0.7646 0.9964 0.0846 0.2339 0.6096 0.4706 0.6881 0.8045 0.1918 0.5795 0.3290 0.7296 0.9661 0.8417 0.4322 0.3425 0.8539 0.1077 0.4708 0.4340 0.5386 0.4330 0.4899 0.2925 0.3641 0.7586 0.9642 0.9607 0.5449 0.2362 0.3217 0.0573 0.5500 0.5746 0.0740 0.8235 0.1572 0.2822 0.3274 0.4756 0.8898 0.5960 0.0507 0.0921 0.1475 0.1726 0.9654 0.9958 0.4072 0.4900 0.9518 0.4734 0.7550 0.4443 0.7000 0.9827 0.4097 0.2692 0.6074 0.0632 0.8178 0.9912 0.7856 0.1898 0.9194 0.8009 0.5920 0.4843 0.8502 0.5559 0.3164 0.8014 0.0337 0.2448 0.6822 0.9138 0.1172 0.7617 0.0669 0.1958 0.1749 0.5044 0.0657 0.3261 0.2607 0.5235 0.2774 0.3124 0.7538 0.7401 0.2160 0.3764 0.1029 0.6114 0.0423 0.9861 0.0539 0.0552 0.0603 0.1992 0.8039 0.2940 0.6013 0.1736 0.8238 0.6903 0.4683 0.0435 0.2153 0.3781 0.3374 0.2396 0.0657 0.8695 0.4397 Columns 20 through 25 0.0297 0.0033 0.2927 0.4264 0.1332 0.2415 0.2785 0.1678 0.4385 0.4717 0.4377 0.0106 0.3901 0.6661 0.8665 0.8876 0.7462 0.0204 0.7250 0.7851 0.9598 0.5317 0.9935 0.3177 0.3933 0.3303 0.7820 0.7344 0.4992 0.7589 0.6290 0.5364 0.9495 0.8766 0.1682 0.5318 0.1893 0.4017 0.1995 0.4424 0.5875 0.8294 0.7882 0.0410 0.4026 0.0419 0.3005 0.2005 0.8363 0.2756 0.6467 0.7691 0.9012 0.1762 0.3174 0.3921 0.7081 0.0927 0.6641 0.8973 (:,:,2) = Columns 1 through 19 0.7295 0.9767 0.9107 0.1318 0.7118 0.9255 0.6909 0.4158 0.2962 0.7614 0.8419 0.1654 0.4302 0.0222 0.1194 0.4118 0.8931 0.6330 0.7192 0.5105 0.3614 0.6143 0.7640 0.8465 0.8773 0.4293 0.6849 0.2809 0.3280 0.1068 0.2722 0.2647 0.3641 0.5543 0.1652 0.7239 0.3740 0.8143 0.5359 0.8440 0.4746 0.5335 0.3952 0.7718 0.3380 0.0489 0.8560 0.7551 0.1411 0.5408 0.4414 0.0513 0.8026 1.0000 0.0751 0.5718 0.8551 0.3204 0.1051 0.8214 0.3956 0.1964 0.0831 0.2789 0.0179 0.6262 0.8167 0.3241 0.2109 0.7512 0.1503 0.8854 0.9613 0.2702 0.2072 0.3297 0.0027 0.0980 0.0290 0.3765 0.0310 0.8001 0.7793 0.5528 0.4243 0.4869 0.9964 0.0846 0.2339 0.6096 0.4706 0.6881 0.8045 0.1918 0.5795 0.6930 0.7368 0.3657 0.7719 0.1206 0.0464 0.1892 0.8462 0.6369 0.9851 0.5386 0.4330 0.4899 0.2925 0.3641 0.7586 0.9642 0.9607 0.5449 0.6023 0.9826 0.5895 0.4044 0.6841 0.8209 0.3883 0.0739 0.0371 0.8075 0.4756 0.8898 0.5960 0.0507 0.0921 0.1475 0.1726 0.9654 0.9958 0.7721 0.1879 0.2124 0.6136 0.5076 0.1983 0.1037 0.2933 0.7815 0.2867 0.6074 0.0632 0.8178 0.9912 0.7856 0.1898 0.9194 0.8009 0.5920 0.0809 0.2578 0.1787 0.9215 0.9610 0.9895 0.7791 0.6516 0.1358 0.7782 0.7617 0.0669 0.1958 0.1749 0.5044 0.0657 0.3261 0.2607 0.5235 0.8553 0.3136 0.3753 0.0828 0.3849 0.4778 0.2705 0.3805 0.5815 0.8126 0.0539 0.0552 0.0603 0.1992 0.8039 0.2940 0.6013 0.1736 0.8238 Columns 20 through 25 0.4322 0.2011 0.2726 0.9072 0.1600 0.8224 0.3968 0.7299 0.7016 0.2639 0.0332 0.9909 0.5188 0.3871 0.0247 0.1508 0.5312 0.5501 0.1601 0.5885 0.8163 0.7043 0.2722 0.3613 0.3290 0.7296 0.9661 0.8417 0.4322 0.3425 0.2362 0.3217 0.0573 0.5500 0.5746 0.0740 0.4072 0.4900 0.9518 0.4734 0.7550 0.4443 0.4843 0.8502 0.5559 0.3164 0.8014 0.0337 0.2774 0.3124 0.7538 0.7401 0.2160 0.3764 0.6903 0.4683 0.0435 0.2153 0.3781 0.3374 (:,:,3) = Columns 1 through 19 0.5720 0.2752 0.6638 0.9715 0.0297 0.0033 0.2927 0.4264 0.1332 0.2415 0.7295 0.9767 0.9107 0.1318 0.7118 0.9255 0.6909 0.4158 0.2962 0.0442 0.9355 0.4878 0.3770 0.2785 0.1678 0.4385 0.4717 0.4377 0.0106 0.5105 0.3614 0.6143 0.7640 0.8465 0.8773 0.4293 0.6849 0.2809 0.1578 0.6124 0.7138 0.7429 0.3901 0.6661 0.8665 0.8876 0.7462 0.0204 0.5359 0.8440 0.4746 0.5335 0.3952 0.7718 0.3380 0.0489 0.8560 0.4253 0.2086 0.1753 0.7646 0.7250 0.7851 0.9598 0.5317 0.9935 0.3177 0.3204 0.1051 0.8214 0.3956 0.1964 0.0831 0.2789 0.0179 0.6262 0.8539 0.1077 0.4708 0.4340 0.3933 0.3303 0.7820 0.7344 0.4992 0.7589 0.0027 0.0980 0.0290 0.3765 0.0310 0.8001 0.7793 0.5528 0.4243 0.8235 0.1572 0.2822 0.3274 0.6290 0.5364 0.9495 0.8766 0.1682 0.5318 0.6930 0.7368 0.3657 0.7719 0.1206 0.0464 0.1892 0.8462 0.6369 0.7000 0.9827 0.4097 0.2692 0.1893 0.4017 0.1995 0.4424 0.5875 0.8294 0.6023 0.9826 0.5895 0.4044 0.6841 0.8209 0.3883 0.0739 0.0371 0.2448 0.6822 0.9138 0.1172 0.7882 0.0410 0.4026 0.0419 0.3005 0.2005 0.7721 0.1879 0.2124 0.6136 0.5076 0.1983 0.1037 0.2933 0.7815 0.1029 0.6114 0.0423 0.9861 0.8363 0.2756 0.6467 0.7691 0.9012 0.1762 0.0809 0.2578 0.1787 0.9215 0.9610 0.9895 0.7791 0.6516 0.1358 0.2396 0.0657 0.8695 0.4397 0.3174 0.3921 0.7081 0.0927 0.6641 0.8973 0.8553 0.3136 0.3753 0.0828 0.3849 0.4778 0.2705 0.3805 0.5815 Columns 20 through 25 0.7614 0.8419 0.1654 0.4302 0.0222 0.1194 0.3280 0.1068 0.2722 0.2647 0.3641 0.5543 0.7551 0.1411 0.5408 0.4414 0.0513 0.8026 0.8167 0.3241 0.2109 0.7512 0.1503 0.8854 0.4869 0.9964 0.0846 0.2339 0.6096 0.4706 0.9851 0.5386 0.4330 0.4899 0.2925 0.3641 0.8075 0.4756 0.8898 0.5960 0.0507 0.0921 0.2867 0.6074 0.0632 0.8178 0.9912 0.7856 0.7782 0.7617 0.0669 0.1958 0.1749 0.5044 0.8126 0.0539 0.0552 0.0603 0.1992 0.8039 (:,:,4) = Columns 1 through 19 0.4118 0.8931 0.6330 0.7192 0.4322 0.2011 0.2726 0.9072 0.1600 0.8224 0.5720 0.2752 0.6638 0.9715 0.0297 0.0033 0.2927 0.4264 0.1332 0.1652 0.7239 0.3740 0.8143 0.3968 0.7299 0.7016 0.2639 0.0332 0.9909 0.0442 0.9355 0.4878 0.3770 0.2785 0.1678 0.4385 0.4717 0.4377 1.0000 0.0751 0.5718 0.8551 0.5188 0.3871 0.0247 0.1508 0.5312 0.5501 0.1578 0.6124 0.7138 0.7429 0.3901 0.6661 0.8665 0.8876 0.7462 0.9613 0.2702 0.2072 0.3297 0.1601 0.5885 0.8163 0.7043 0.2722 0.3613 0.4253 0.2086 0.1753 0.7646 0.7250 0.7851 0.9598 0.5317 0.9935 0.6881 0.8045 0.1918 0.5795 0.3290 0.7296 0.9661 0.8417 0.4322 0.3425 0.8539 0.1077 0.4708 0.4340 0.3933 0.3303 0.7820 0.7344 0.4992 0.7586 0.9642 0.9607 0.5449 0.2362 0.3217 0.0573 0.5500 0.5746 0.0740 0.8235 0.1572 0.2822 0.3274 0.6290 0.5364 0.9495 0.8766 0.1682 0.1475 0.1726 0.9654 0.9958 0.4072 0.4900 0.9518 0.4734 0.7550 0.4443 0.7000 0.9827 0.4097 0.2692 0.1893 0.4017 0.1995 0.4424 0.5875 0.1898 0.9194 0.8009 0.5920 0.4843 0.8502 0.5559 0.3164 0.8014 0.0337 0.2448 0.6822 0.9138 0.1172 0.7882 0.0410 0.4026 0.0419 0.3005 0.0657 0.3261 0.2607 0.5235 0.2774 0.3124 0.7538 0.7401 0.2160 0.3764 0.1029 0.6114 0.0423 0.9861 0.8363 0.2756 0.6467 0.7691 0.9012 0.2940 0.6013 0.1736 0.8238 0.6903 0.4683 0.0435 0.2153 0.3781 0.3374 0.2396 0.0657 0.8695 0.4397 0.3174 0.3921 0.7081 0.0927 0.6641 Columns 20 through 25 0.2415 0.7295 0.9767 0.9107 0.1318 0.7118 0.0106 0.5105 0.3614 0.6143 0.7640 0.8465 0.0204 0.5359 0.8440 0.4746 0.5335 0.3952 0.3177 0.3204 0.1051 0.8214 0.3956 0.1964 0.7589 0.0027 0.0980 0.0290 0.3765 0.0310 0.5318 0.6930 0.7368 0.3657 0.7719 0.1206 0.8294 0.6023 0.9826 0.5895 0.4044 0.6841 0.2005 0.7721 0.1879 0.2124 0.6136 0.5076 0.1762 0.0809 0.2578 0.1787 0.9215 0.9610 0.8973 0.8553 0.3136 0.3753 0.0828 0.3849 (:,:,5) = Columns 1 through 19 0.9255 0.6909 0.4158 0.2962 0.7614 0.8419 0.1654 0.4302 0.0222 0.1194 0.4118 0.8931 0.6330 0.7192 0.4322 0.2011 0.2726 0.9072 0.1600 0.8773 0.4293 0.6849 0.2809 0.3280 0.1068 0.2722 0.2647 0.3641 0.5543 0.1652 0.7239 0.3740 0.8143 0.3968 0.7299 0.7016 0.2639 0.0332 0.7718 0.3380 0.0489 0.8560 0.7551 0.1411 0.5408 0.4414 0.0513 0.8026 1.0000 0.0751 0.5718 0.8551 0.5188 0.3871 0.0247 0.1508 0.5312 0.0831 0.2789 0.0179 0.6262 0.8167 0.3241 0.2109 0.7512 0.1503 0.8854 0.9613 0.2702 0.2072 0.3297 0.1601 0.5885 0.8163 0.7043 0.2722 0.8001 0.7793 0.5528 0.4243 0.4869 0.9964 0.0846 0.2339 0.6096 0.4706 0.6881 0.8045 0.1918 0.5795 0.3290 0.7296 0.9661 0.8417 0.4322 0.0464 0.1892 0.8462 0.6369 0.9851 0.5386 0.4330 0.4899 0.2925 0.3641 0.7586 0.9642 0.9607 0.5449 0.2362 0.3217 0.0573 0.5500 0.5746 0.8209 0.3883 0.0739 0.0371 0.8075 0.4756 0.8898 0.5960 0.0507 0.0921 0.1475 0.1726 0.9654 0.9958 0.4072 0.4900 0.9518 0.4734 0.7550 0.1983 0.1037 0.2933 0.7815 0.2867 0.6074 0.0632 0.8178 0.9912 0.7856 0.1898 0.9194 0.8009 0.5920 0.4843 0.8502 0.5559 0.3164 0.8014 0.9895 0.7791 0.6516 0.1358 0.7782 0.7617 0.0669 0.1958 0.1749 0.5044 0.0657 0.3261 0.2607 0.5235 0.2774 0.3124 0.7538 0.7401 0.2160 0.4778 0.2705 0.3805 0.5815 0.8126 0.0539 0.0552 0.0603 0.1992 0.8039 0.2940 0.6013 0.1736 0.8238 0.6903 0.4683 0.0435 0.2153 0.3781 Columns 20 through 25 0.8224 0.5720 0.2752 0.6638 0.9715 0.0297 0.9909 0.0442 0.9355 0.4878 0.3770 0.2785 0.5501 0.1578 0.6124 0.7138 0.7429 0.3901 0.3613 0.4253 0.2086 0.1753 0.7646 0.7250 0.3425 0.8539 0.1077 0.4708 0.4340 0.3933 0.0740 0.8235 0.1572 0.2822 0.3274 0.6290 0.4443 0.7000 0.9827 0.4097 0.2692 0.1893 0.0337 0.2448 0.6822 0.9138 0.1172 0.7882 0.3764 0.1029 0.6114 0.0423 0.9861 0.8363 0.3374 0.2396 0.0657 0.8695 0.4397 0.3174 (:,:,6) = Columns 1 through 19 0.0033 0.2927 0.4264 0.1332 0.2415 0.7295 0.9767 0.9107 0.1318 0.7118 0.9255 0.6909 0.4158 0.2962 0.7614 0.8419 0.1654 0.4302 0.0222 0.1678 0.4385 0.4717 0.4377 0.0106 0.5105 0.3614 0.6143 0.7640 0.8465 0.8773 0.4293 0.6849 0.2809 0.3280 0.1068 0.2722 0.2647 0.3641 0.6661 0.8665 0.8876 0.7462 0.0204 0.5359 0.8440 0.4746 0.5335 0.3952 0.7718 0.3380 0.0489 0.8560 0.7551 0.1411 0.5408 0.4414 0.0513 0.7851 0.9598 0.5317 0.9935 0.3177 0.3204 0.1051 0.8214 0.3956 0.1964 0.0831 0.2789 0.0179 0.6262 0.8167 0.3241 0.2109 0.7512 0.1503 0.3303 0.7820 0.7344 0.4992 0.7589 0.0027 0.0980 0.0290 0.3765 0.0310 0.8001 0.7793 0.5528 0.4243 0.4869 0.9964 0.0846 0.2339 0.6096 0.5364 0.9495 0.8766 0.1682 0.5318 0.6930 0.7368 0.3657 0.7719 0.1206 0.0464 0.1892 0.8462 0.6369 0.9851 0.5386 0.4330 0.4899 0.2925 0.4017 0.1995 0.4424 0.5875 0.8294 0.6023 0.9826 0.5895 0.4044 0.6841 0.8209 0.3883 0.0739 0.0371 0.8075 0.4756 0.8898 0.5960 0.0507 0.0410 0.4026 0.0419 0.3005 0.2005 0.7721 0.1879 0.2124 0.6136 0.5076 0.1983 0.1037 0.2933 0.7815 0.2867 0.6074 0.0632 0.8178 0.9912 0.2756 0.6467 0.7691 0.9012 0.1762 0.0809 0.2578 0.1787 0.9215 0.9610 0.9895 0.7791 0.6516 0.1358 0.7782 0.7617 0.0669 0.1958 0.1749 0.3921 0.7081 0.0927 0.6641 0.8973 0.8553 0.3136 0.3753 0.0828 0.3849 0.4778 0.2705 0.3805 0.5815 0.8126 0.0539 0.0552 0.0603 0.1992 Columns 20 through 25 0.1194 0.4118 0.8931 0.6330 0.7192 0.4322 0.5543 0.1652 0.7239 0.3740 0.8143 0.3968 0.8026 1.0000 0.0751 0.5718 0.8551 0.5188 0.8854 0.9613 0.2702 0.2072 0.3297 0.1601 0.4706 0.6881 0.8045 0.1918 0.5795 0.3290 0.3641 0.7586 0.9642 0.9607 0.5449 0.2362 0.0921 0.1475 0.1726 0.9654 0.9958 0.4072 0.7856 0.1898 0.9194 0.8009 0.5920 0.4843 0.5044 0.0657 0.3261 0.2607 0.5235 0.2774 0.8039 0.2940 0.6013 0.1736 0.8238 0.6903 (:,:,7) = Columns 1 through 19 0.2011 0.2726 0.9072 0.1600 0.8224 0.5720 0.2752 0.6638 0.9715 0.0297 0.0033 0.2927 0.4264 0.1332 0.2415 0.7295 0.9767 0.9107 0.1318 0.7299 0.7016 0.2639 0.0332 0.9909 0.0442 0.9355 0.4878 0.3770 0.2785 0.1678 0.4385 0.4717 0.4377 0.0106 0.5105 0.3614 0.6143 0.7640 0.3871 0.0247 0.1508 0.5312 0.5501 0.1578 0.6124 0.7138 0.7429 0.3901 0.6661 0.8665 0.8876 0.7462 0.0204 0.5359 0.8440 0.4746 0.5335 0.5885 0.8163 0.7043 0.2722 0.3613 0.4253 0.2086 0.1753 0.7646 0.7250 0.7851 0.9598 0.5317 0.9935 0.3177 0.3204 0.1051 0.8214 0.3956 0.7296 0.9661 0.8417 0.4322 0.3425 0.8539 0.1077 0.4708 0.4340 0.3933 0.3303 0.7820 0.7344 0.4992 0.7589 0.0027 0.0980 0.0290 0.3765 0.3217 0.0573 0.5500 0.5746 0.0740 0.8235 0.1572 0.2822 0.3274 0.6290 0.5364 0.9495 0.8766 0.1682 0.5318 0.6930 0.7368 0.3657 0.7719 0.4900 0.9518 0.4734 0.7550 0.4443 0.7000 0.9827 0.4097 0.2692 0.1893 0.4017 0.1995 0.4424 0.5875 0.8294 0.6023 0.9826 0.5895 0.4044 0.8502 0.5559 0.3164 0.8014 0.0337 0.2448 0.6822 0.9138 0.1172 0.7882 0.0410 0.4026 0.0419 0.3005 0.2005 0.7721 0.1879 0.2124 0.6136 0.3124 0.7538 0.7401 0.2160 0.3764 0.1029 0.6114 0.0423 0.9861 0.8363 0.2756 0.6467 0.7691 0.9012 0.1762 0.0809 0.2578 0.1787 0.9215 0.4683 0.0435 0.2153 0.3781 0.3374 0.2396 0.0657 0.8695 0.4397 0.3174 0.3921 0.7081 0.0927 0.6641 0.8973 0.8553 0.3136 0.3753 0.0828 Columns 20 through 25 0.7118 0.9255 0.6909 0.4158 0.2962 0.7614 0.8465 0.8773 0.4293 0.6849 0.2809 0.3280 0.3952 0.7718 0.3380 0.0489 0.8560 0.7551 0.1964 0.0831 0.2789 0.0179 0.6262 0.8167 0.0310 0.8001 0.7793 0.5528 0.4243 0.4869 0.1206 0.0464 0.1892 0.8462 0.6369 0.9851 0.6841 0.8209 0.3883 0.0739 0.0371 0.8075 0.5076 0.1983 0.1037 0.2933 0.7815 0.2867 0.9610 0.9895 0.7791 0.6516 0.1358 0.7782 0.3849 0.4778 0.2705 0.3805 0.5815 0.8126
  6 Comments
Francois Miguet
Francois Miguet on 12 Apr 2023
Edited: Adam Danz on 13 Apr 2023
@Vilém Frynta weirdly it is the only option I see...
Walter Roberson
Walter Roberson on 12 Apr 2023
(I moved responses around so you can accept the posting from @Vilém Frynta )

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!