How to speed up data processing when extracting from a large cell array?

I am dealing with a large cell array (e.g., 3826341x1 cell). I would like merge the data or matrix from each cell. Using 'cell2mat' takes so much of time. Is there any alternative to cell2mat to process the data faster? Any leads will be highly appreciated. Thanks.

5 件のコメント

What about vertcat
mycell = {2, 3, 4}
mycell = 1×3 cell array
{[2]} {[3]} {[4]}
mymat = vertcat(mycell{:})
mymat = 3×1
2 3 4
Raju Kumar
Raju Kumar 2023 年 9 月 19 日
Thanks @Ive J. I have also checked using vertcat. It does reduce the processing time but not significantly. I think the array is quite huge.
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 19 日
It would be better if you can attach a sample of your data, so that we can know how the data is stored inside the cell.
Raju Kumar
Raju Kumar 2023 年 9 月 19 日
Hi @Dyuman Joshi, I have attached a file of small size. The actaul file is a way bigger, but can not be uploadded here because Matlab allows only up to 5MB.
With the inbuilt functions, vertcat is the fastest option.
load('array.mat')
whos
Name Size Bytes Class Attributes alphaClusterAll 14879x1 24311128 cell ans 1x34 68 char cmdout 1x33 66 char
f1=@(x) cell2mat(x);
f2=@(x) vertcat(x{:});
f3=@(x) cat(1,x{:});
F1 = @()f1(alphaClusterAll);
F2 = @()f2(alphaClusterAll);
F3 = @()f3(alphaClusterAll);
fprintf('Time taken by cell2mat = %f seconds', timeit(F1))
Time taken by cell2mat = 0.009412 seconds
fprintf('Time taken by vertcat = %f seconds', timeit(F2))
Time taken by vertcat = 0.003332 seconds
fprintf('Time taken by cat = %f seconds', timeit(F3))
Time taken by cat = 0.005746 seconds
isequal(F1(),F2(),F3())
ans = logical
1
You can try this FEX submission - Cell2Vec

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2021a

質問済み:

2023 年 9 月 19 日

コメント済み:

2023 年 9 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by