フィルターのクリア

How to calculate the mean of a certain column of tables inside a cell

5 ビュー (過去 30 日間)
BN
BN 2020 年 5 月 4 日
回答済み: dpb 2020 年 5 月 4 日
Dear all,
I have a 1x3 cell, with tables as it's arrays. I want to have a mean of first column of each table. So the output I need is a 1x3 array.
I tried to do it in this way:
test_means = cellfun(@mean,test,'uni',0);
but I get this error:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
And:
test_means = cellfun(@mean,test{:,:}(:,1),'uni',0);
But another error appeared:
Expected one output from a curly brace or dot indexing expression, but
there were 3 results.
Thank you so much

採用された回答

dpb
dpb 2020 年 5 月 4 日
Close but not quite right syntax...many ways you can do, but
test_means = cellfun(@(x) mean(x.Var1),test);
should do the trick presuming you maintained the same variable names for each table.
Use the proper variable name for your table, of course, not the default Var1 unless it is the right name, of course.
cellfun passes the content of the cell; in this case this is still a table so have to dereference it.

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 4 日
try this
load test
mean_val = cellfun(@(x) mean(x{:,1}), test)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by