Applying functions to each cell in a cell array

11 ビュー (過去 30 日間)
Benedict Low
Benedict Low 2017 年 8 月 4 日
コメント済み: Benedict Low 2017 年 8 月 6 日
Good afternoon,
I have a 60 by 1 cell array that stores matrices of different sizes (all numeric). I want to perform certain functions (mean, diff, hypot, convhull...etc) on these matrices from the 1st element of the cell array, to the 60th element.
Is there a way I can do that?
Thank you.
  3 件のコメント
Jan
Jan 2017 年 8 月 4 日
Did you read the answers, which have been given already?
Benedict Low
Benedict Low 2017 年 8 月 6 日
Yes I did Jan. But ran into errors. Trying to fix them now.

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

採用された回答

the cyclist
the cyclist 2017 年 8 月 4 日
編集済み: the cyclist 2017 年 8 月 4 日
cellfun does exactly this.

その他の回答 (2 件)

Moses Wayne
Moses Wayne 2017 年 8 月 4 日
The function call "cellfun" is a useful function in this case. In the case of the function mean, the code below would work to get the mean of each subset within the cell array:
myCell = {[0 1 2]; [3 4 5]; [6 7 8]; [9 10 11]}
result = cellfun(@mean,myCell)
In this case, result would be an array of values [1 4 7 10]. I've linked the documentation to "cellfun" here.

Adam
Adam 2017 年 8 月 4 日
If you have multiple functions to perform a for loop would likely be simplest. The idea that for loops are very slow in Matlab is often erroneous. I favour cellfun and arrayfun from an aesthetic perspective, but from a speed perspective for loops are usually faster. And certainly if you want to calculate multiple things then a single pass over the data in a for loop is a lot better than multiple calls to cellfun, though you could of course create a composite function that will calculate all your results on a single cell and then call this via cellfun if you prefer the cellfun approach.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by