Is it possible to write methods that iteratively process each element of an objectarray without writing for-loops each time?

If I have such an objectarray:
classdef ObjectArray
properties
Value
end
methods
function obj = ObjectArray(F)
if nargin ~= 0
m = size(F,1);
n = size(F,2);
obj(m,n) = obj;
for i = 1:m
for j = 1:n
obj(i,j).Value = F;
end
end
end
end
end
end
An instance A is created as:
F = magic(5);
A = ObjectArray(F);
And I would like to calculate the mean, the max, or do whatever calculations based on each obj.Value, then is it possible to write methods without iterations in each of them?
I expect something like this:
function obj = get.mean(obj)
obj.mean = mean(obj.value)
end
but apparently it doesn't work.

 採用された回答

Matt J
Matt J 2021 年 4 月 8 日
編集済み: Matt J 2021 年 4 月 8 日
It is not possible in a get.property() method, but it is possible in a normal method.
function meanValues = mean(Objects)
meanValues = arrayfun( @(ob)mean(ob.Value,'all') , Objects);
end

2 件のコメント

zhehao.nkd
zhehao.nkd 2021 年 4 月 8 日
編集済み: zhehao.nkd 2021 年 4 月 8 日
Thank you so much! So at least (1) a function handle and (2) arrayfun() is needed, just like how a normal cell object was treated.
Thank you so much!
You're quite welcome, but please click "Accept" if this addressed your problem.
So at least (1) a function handle and (2) arrayfun() is needed, just like how a normal cell object was treated.
It is one solution. If obj.values are all matrices of the same size, you could also have done ,
function meanValues = mean(Objects)
meanValues=mean( cat(3,Objects.Values) , [1,2]);
meanValues=reshape(meanValues,size(Objects));
end

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFinancial Toolbox についてさらに検索

製品

リリース

R2019a

質問済み:

2021 年 4 月 8 日

コメント済み:

2021 年 4 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by