Info

This question is closed. Reopen it to edit or answer.

Logical indexing 2d to 3d without a for loop

1 view (last 30 days)
Tyler
Tyler on 26 Apr 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, this may be a simple question, but any help would be appreciated.
I have an array g = 100 x 5, and a much larger array w = 10000 x 10. I would like to have a 3D array, k = 100 x 10 x 5, where the first page (:,:,1) is the first column of g(:,1) indexing each column of w(:,:). Then my second page (:,:,2) is the second column of g(:,2) indexing each column of w, etc. If possible I would like to avoid a for loop, as I would like to increase the third dimension of k from 5 to 100 or 1000, but for now I would like to make sure I can do it. If a for loop is the only option I have, then that is fine. So far I have tried this:
for i = 1:5
k(:,:,i) = w(g(:,i),:);
end
Does this work? Or is there a better way to do this?
Thank you for the help!

Answers (0)

Community Treasure Hunt

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

Start Hunting!