MATLAB: How to index using cell arrays cell arraysindexing A = randi(100,1,8); % Some array that I want to index into. B = {[1;2;3];[4;5];[6];[7;8]}; % Cell array with indices. %I want to index B into A to get: C = {[A(1);A(2);A(3)];…;[A(7);A(8)]} Best Answer cellfun(@(b) A(b), B, 'UniformOutput', false) Related SolutionsMATLAB: How to create a 1×3 cell array to hold multiple individual arrays my_cell_array = {a,b,c};Or if you are simply asking how to pre-allocate the cell array in advance:my_cell_array = cell(1,3); MATLAB: How to access an cell array using an cell array of indices Using cellfun (not faster, just more compact):>> A = {[1,2,3],[4,5,6,7,8],[9,10,11,12]};>> B = {1:2,2:4,1:3};>> C = cellfun(@(a,b)a(b),A,B,'uni',0);>> C{:}ans = 1 2ans = 5 6 7ans = 9 10 11 Related QuestionReshaping / regrouping cell arraysDelete matrix in cellTwo cell arrays indexingHow can prelocate arraysFinding row index an element in cell arrayHow to manipulate arrays
Best Answer