[英]Extracting data from a 3D cell array into a 2D Matrix - MATLAB
我有一個三維數組,即16x10x3,我希望將:,:,1
:,:,2
和:,:,3
所有值分別提取到矩陣的一列中。
我考慮過預先分配一個矩陣,然后運行一個基本的循環,例如:
for m=1:3
mat(:,m) = ([a{:,:,m}]);
end
有沒有一種更有效的方法,而不必依賴循環?
編輯::,:,1/2/3之間有不同數量的值。
現在是時候進入bsxfun
! 這是實現-
%// Get the number of elements in each column of the input cell array
lens = sum(cellfun('length',reshape(a,[],size(a,3))),1)
%// Store the maximum number of elements possible in any column of output array
max_lens = max(lens)
%// Setup output array, with no. of rows as max number of elements in each column
%// and no. of columns would be same as the no. of columns in input cell array
mat = zeros(max_lens,numel(lens))
%// Create as mask that has ones to the "extent" of number of elements in
%// each column of the input cell array using the lengths
mask = bsxfun(@le,[1:max_lens]',lens) %//'
%// Finally, store the values from input cell array into masked positions
mat(mask) = [a{:}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.