Using vertcat for cell array of differing length

23 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 5 月 4 日
編集済み: Jonas 2022 年 5 月 4 日
I have a 3 x 2 cell array consisting of elements that are vectors 1 x N where N changes. How do I find the vector of minimum length in the cell array and vertically concatenate based on the length of that vector?
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 4 日
Can you show the data?
Jonas
Jonas 2022 年 5 月 4 日
編集済み: Jonas 2022 年 5 月 4 日
it also depends, if you want the minimum length per cell column or if you want all vectors to be cut down to the smallest vector length of all cells. start with
lengthMatrix=cellfun(@numel,yourCell)
to get a matrix of vector lengths, then continue with eg
minLength=min(lengthMatrix,[],'all')
cellWithCutEntries=cellfun(@(in) in(1:minLength),yourCell)
and a
cell2mat(cellWithCutEntries)
call at the end
this concatenates all vectors off all cells using the minimum length

サインインしてコメントする。

回答 (1 件)

KSSV
KSSV 2022 年 5 月 4 日
編集済み: KSSV 2022 年 5 月 4 日
% Make dummy data for demo
A = cell(3,2) ;
for i = 1:3
for j = 1:2
N = randperm(10,1) ;
A{i,j} = rand(1,N) ;
end
end
% Get length
L = cellfun(@numel,A) ;
% Join L < 5 arrays
idx = L < 5 ; % GEt indices of cell arrays whos length is less than 5
B = A(idx) ;
iwant = horzcat(B{:}) % join the cell arrays

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by