首先,我想你的意思是定義一個多維結構:
a(1).field1=[1:5];
a(2).field1=[1:6];
a(1).field2=[2:8];
a(2).field2=[2:9];
(注圓括號而不是花括號大括號會給你包含兩個單元陣列struct
s)。現在,值,您尋找:
max_mean = cellfun(@(x)[max(x) mean(x)], {a.field1}, 'UniformOutput', false);
這樣做,會給你maximim和max_mean{1}
的a(1).field1
平均,最大和max_mean{2}
的a(2).field1
意思。
這樣做的所有字段可以由另一個cellfun
嵌套cellfun
以上來完成:
max_means = cellfun(@(x) ...
cellfun(@(y)[max(y) mean(y)], {a.(x)}, 'UniformOutput', false), ...
fieldnames(a), 'UniformOutput', false);
使
max_means{1}{1} % will give you the max's and means of a(1).field1
max_means{1}{2} % will give you the max's and means of a(2).field1
max_means{2}{1} % will give you the max's and means of a(1).field2
max_means{2}{2} % will give you the max's and means of a(2).field2
發揮這些功能,直到您找到適合您的需求。
謝謝。這很好,很簡單。嵌套cellfun解決方案會更快嗎? – Dominik