How to take elements from a multidimensional array along the page axis?

Hello,
I want to map a 3d array to a 2d array by taking a single value along the page axis at each coordinate.
For example, in python I would do the following:
% x = np.linspace(0, 1, 50)
% X, Y = np.meshgrid(x, x)
% Z = np.exp(-X**2/2) + np.exp(-Y**2/2)
% A = np.arange(10)[:,np.newaxis,np.newaxis] * Z
%
% I, J = np.mgrid[0:50, 0:50]
% choice_idx = np.randint(10, size=(50, 50))
% choice_arr = A[choice_idx,I,J]
However, applying this naivly to matlab gives me "Requested array exceeds the maximum possible variable size.":
x = linspace(0, 1, 50);
[X, Y] = meshgrid(x, x);
Z = exp(-X.^2/2) + exp(-Y.^2/2);
A = reshape(0:9, 1, 1, []) .* Z;
[I, J] = meshgrid(1:50, 1:50);
choice_idx = randi(10, 50, 50);
choice_arr = A(I,J,choice_idx)
Requested 2500x2500x2500 (116.4GB) array exceeds maximum array size preference (31.0GB). This might cause MATLAB to become unresponsive.
How can I achieve this in matlab? Apologies, if this question was asked before.

 Respuesta aceptada

By a single page do you mean all rows, all columns of one index in the 3rd dimension (see here)? In MALAB you would use the colon operator to specify all rows/columns, and then specify the index of the 3rd dimension
x = linspace(0, 1, 50);
[X, Y] = meshgrid(x, x);
Z = exp(-X.^2/2) + exp(-Y.^2/2);
A = reshape(0:9, 1, 1, []) .* Z;
choice_arr = A(:,:,10)
choice_arr = 50×50
18.0000 17.9981 17.9925 17.9831 17.9701 17.9533 17.9328 17.9086 17.8808 17.8495 17.8145 17.7761 17.7341 17.6888 17.6400 17.5880 17.5328 17.4743 17.4128 17.3482 17.2807 17.2103 17.1371 17.0612 16.9827 16.9016 16.8182 16.7324 16.6443 16.5541 17.9981 17.9963 17.9906 17.9813 17.9682 17.9514 17.9309 17.9068 17.8790 17.8476 17.8126 17.7742 17.7322 17.6869 17.6382 17.5862 17.5309 17.4725 17.4109 17.3463 17.2788 17.2084 17.1352 17.0593 16.9808 16.8998 16.8163 16.7305 16.6424 16.5522 17.9925 17.9906 17.9850 17.9757 17.9626 17.9458 17.9253 17.9011 17.8734 17.8420 17.8070 17.7686 17.7266 17.6813 17.6326 17.5805 17.5253 17.4668 17.4053 17.3407 17.2732 17.2028 17.1296 17.0537 16.9752 16.8941 16.8107 16.7249 16.6368 16.5466 17.9831 17.9813 17.9757 17.9663 17.9532 17.9364 17.9159 17.8918 17.8640 17.8326 17.7977 17.7592 17.7173 17.6719 17.6232 17.5712 17.5159 17.4575 17.3959 17.3314 17.2638 17.1934 17.1202 17.0443 16.9658 16.8848 16.8013 16.7155 16.6274 16.5372 17.9701 17.9682 17.9626 17.9532 17.9401 17.9233 17.9028 17.8787 17.8509 17.8195 17.7846 17.7461 17.7042 17.6588 17.6101 17.5581 17.5028 17.4444 17.3828 17.3183 17.2507 17.1803 17.1072 17.0313 16.9527 16.8717 16.7882 16.7024 16.6144 16.5242 17.9533 17.9514 17.9458 17.9364 17.9233 17.9065 17.8860 17.8619 17.8341 17.8027 17.7678 17.7293 17.6874 17.6420 17.5933 17.5413 17.4860 17.4276 17.3661 17.3015 17.2340 17.1636 17.0904 17.0145 16.9359 16.8549 16.7714 16.6856 16.5976 16.5074 17.9328 17.9309 17.9253 17.9159 17.9028 17.8860 17.8656 17.8414 17.8136 17.7822 17.7473 17.7088 17.6669 17.6215 17.5728 17.5208 17.4655 17.4071 17.3456 17.2810 17.2135 17.1431 17.0699 16.9940 16.9155 16.8344 16.7509 16.6651 16.5771 16.4869 17.9086 17.9068 17.9011 17.8918 17.8787 17.8619 17.8414 17.8173 17.7895 17.7581 17.7231 17.6847 17.6427 17.5974 17.5487 17.4967 17.4414 17.3830 17.3214 17.2568 17.1893 17.1189 17.0457 16.9698 16.8913 16.8103 16.7268 16.6410 16.5529 16.4627 17.8808 17.8790 17.8734 17.8640 17.8509 17.8341 17.8136 17.7895 17.7617 17.7303 17.6954 17.6569 17.6150 17.5696 17.5209 17.4689 17.4136 17.3552 17.2936 17.2291 17.1615 17.0911 17.0179 16.9420 16.8635 16.7825 16.6990 16.6132 16.5251 16.4349 17.8495 17.8476 17.8420 17.8326 17.8195 17.8027 17.7822 17.7581 17.7303 17.6989 17.6640 17.6255 17.5836 17.5382 17.4895 17.4375 17.3822 17.3238 17.2622 17.1977 17.1301 17.0597 16.9866 16.9107 16.8321 16.7511 16.6676 16.5818 16.4938 16.4036

3 comentarios

Thank you for the quick reply!
What I want is a "mixture" of these 10 matrices, specified by a different page index at each coordinate.
Perhaps in terms of a for-loop:
choice_idx = randi(10, 50, 50);
choice_arr = zeros(50, 50);
for i=1:50
for j=1:50
choice_arr(i,j) = A(i,j,choice_idx(i,j));
end
end
For large arrays this is obviously very inefficient, so I'm curious whether this can be achieved in a vectorized fashion.
There may be other ways to do this, but I think I would use linear indexing to do what you want. See here and the relevant section here. I use the sub2ind function below to convert subscripts to linear indexing.
x = linspace(0, 1, 50);
[X, Y] = meshgrid(x, x);
Z = exp(-X.^2/2) + exp(-Y.^2/2);
A = reshape(0:9, 1, 1, []) .* Z;
[I, J] = meshgrid(1:50, 1:50);
choice_idx = randi(10, 50, 50);
% obrain linear indeces of desired elements
ind = sub2ind(size(A),I,J,choice_idx);
% extract and reshape back to 50x50
choice_arr = reshape(A(ind),50,50)
choice_arr = 50×50
6.0000 0 3.9983 3.9963 17.9701 9.9740 11.9552 1.9898 11.9206 17.8495 3.9588 7.9005 1.9705 17.6888 9.8000 11.7254 9.7404 11.6496 15.4780 3.8552 17.2807 0 11.4247 15.1655 15.0957 11.2678 13.0808 14.8732 9.2468 5.5180 9.9990 1.9996 17.9906 1.9979 1.9965 7.9784 3.9846 1.9896 5.9597 9.9153 13.8543 1.9749 15.7620 17.6869 5.8794 13.6781 3.8958 9.7069 3.8691 1.9274 7.6795 7.6482 17.1352 13.2684 9.4338 0 5.6054 11.1537 9.2458 7.3565 13.9942 5.9969 7.9933 11.9838 9.9792 5.9819 13.9419 17.9011 13.9015 0 0 1.9743 1.9696 5.8938 9.7959 17.5805 3.8945 11.6446 0 7.7070 5.7577 17.2028 7.6132 13.2640 1.8861 5.6314 9.3393 9.2916 11.0912 11.0311 17.9831 0 7.9892 13.9738 3.9896 13.9505 13.9346 11.9279 0 9.9070 0 11.8395 1.9686 15.7084 13.7069 7.8094 13.6235 11.6383 0 15.4057 3.8364 11.4623 7.6090 17.0443 11.3106 3.7522 1.8668 0 0 0 9.9834 13.9753 5.9875 17.9532 9.9667 0 1.9892 1.9865 13.8840 13.8596 13.8324 7.8872 5.9014 9.8105 11.7401 7.8036 3.8895 7.7531 15.4514 15.3940 0 1.9089 11.4048 0 16.9527 16.8717 9.3268 3.7116 11.0762 12.8521 11.9688 9.9730 7.9759 15.9435 17.9233 17.9065 17.8860 9.9233 7.9263 0 11.8452 17.7293 5.8958 7.8409 9.7741 0 5.8287 0 17.3661 0 13.4042 3.8141 1.8989 7.5620 16.9359 13.1094 7.4540 7.4158 16.5976 1.8342 3.9851 11.9539 1.9917 5.9720 7.9568 17.8860 11.9104 9.9119 7.9172 15.8064 13.8035 17.7088 11.7779 15.6636 13.6678 0 1.9406 3.8682 1.9273 17.2810 9.5630 17.1431 15.1732 7.5529 0 5.6115 1.8612 12.9618 0 9.1594 1.9898 5.9689 17.9011 17.8918 13.9056 11.9079 13.8767 15.8376 3.9532 17.7581 11.8154 13.7548 1.9603 9.7763 3.8997 7.7763 0 1.9314 5.7738 11.5046 3.8198 17.1189 15.1518 15.0843 11.2609 3.7356 13.0097 7.3960 5.5176 5.4876 7.9470 17.8790 7.9437 13.8942 1.9834 7.9263 0 17.7895 11.8411 15.7603 5.8985 3.9238 7.8289 5.8565 13.6274 3.8820 0 15.4268 3.8430 0 3.8137 11.3941 3.7818 0 13.1161 0 16.6990 0 1.8361 7.3044 5.9498 3.9661 17.8420 13.8698 7.9198 3.9562 3.9516 9.8656 3.9401 11.7993 7.8507 1.9584 3.9075 3.8974 13.6030 9.6875 3.8627 11.5492 1.9180 0 3.8067 13.2687 5.6622 16.9107 14.9619 9.3062 1.8520 7.3697 1.8326 7.2905
Thanks, this is exactly what I need!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 1 de En. de 2022

Comentada:

el 1 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by