Extracting a specific element from each cell within cell array

前端 未结 3 677
轻奢々
轻奢々 2021-01-21 01:24

I have a cell array A of size 10x10 (say). Each cell in turn contains a 5x20 matrix. I want to select (i,j) element from each

3条回答
  •  终归单人心
    2021-01-21 02:12

    If memory is not an issue, you can concat all matrices along a third dim; and then indexing is very easy:

    %// Example data
    A = {[1 2;5 6], [3 4; 6 7]; [3 4; 6 7], [9 8; 5 6]};
    ii = 2;
    jj = 1;
    
    %// Compute the result B
    A2 = cat(3,A{:}); %// concat along third dim
    B = reshape(A2(ii,jj,:),size(A{1})); %// index A2 and reshape
    

提交回复
热议问题