How to take elements from a multidimensional array along the page axis?
Show older comments
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)
How can I achieve this in matlab? Apologies, if this question was asked before.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!