Vectorized Data Extraction from Cell Array

I have a Nx1 cell array wherein each cell is a #x2 double array (i.e. each array within the cell is of variable length). CellArray{n}(:,1) contains a list of data indecis, while CellArray{n}(:,2) contains values associated with those indecis. I want to get a unique list of all values in that first column from all cells, then perform operations on each value associated with a given data indexi accross all cells, e.g.:
Indecis = unique(CellArray{:}(:,1))
Results = mean(CellArray{:}(Indecis,2))
The above is, of course, not proper MatLab syntax, but I hope it conveys what I'm trying to do. I can achive this rather simply by using cell2mat,
DataMatrix = cell2mat(CellArray);
But whos is telling me that DataMatrix is waaaaaaay bigger than CellArray. As such, I'd like to know if there is a less data/processor intensive way to do this (e.g. cellfun with an annonymous function handle?).

4 件のコメント

Matt J
Matt J 2022 年 9 月 19 日
But whos is telling me that DataMatrix is waaaaaaay bigger than CellArray.
It shouldn't be. It should consume about the same amount of RAM.
Gabriel Stanley
Gabriel Stanley 2022 年 9 月 19 日
編集済み: Gabriel Stanley 2022 年 9 月 19 日
aaaaand running it again they returned the approximately the same results. I got confused across multiple iterations of the code which feeds CellArray.
And thinking about it, I don't think there is a better way to do what I'm attempting. Your prior suggestion returned a cell array of the same length as CellArray, whereas what I was loking for was:
Results(:,1) = unique(CellArray{:}(:,1)), i.e. a unique list of all data indecis in the entire CellArray, of type double
and
Results(n,2) = F(CellArray{1}(Results(n,1),2),CellArray{2}(Results(n,1),2),...CellArray{end}(Results(n,1),2))
where F is any function, and the "..." is used in the mathematical sense.
If there are no functions which can handle multiple arrays as a single array without creating a temporary, concatenated copy of those multiple arrays, I think I may be stuck with cell2mat.
dpb
dpb 2022 年 9 月 19 日
Just a nit-picky note that in English, the plural of "index" is "indices"...
Image Analyst
Image Analyst 2022 年 9 月 19 日
編集済み: Image Analyst 2022 年 9 月 20 日
@dpb the language has evolved since you and I were young. I think the "ices" may have been a holdover from Latin but now "indexes" has evolved into a perfectly acceptable plural of index. See https://www.dictionary.com/browse/index So both "indices" and "indexes" are correct, however, "indecis" is not correct.
I've also heard that "whom" is going away, in favor of a simpler "who", and maybe considered almost archaic in a few decades, if not already.
Speaking of simplification:
Yay for simpler! 😃

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2019b

質問済み:

2022 年 9 月 19 日

編集済み:

2022 年 9 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by