1. I have a class A with property b.
  2. I make an n-by-m array with objects of class A
  3. I would like to, for instance, find the mean of all the rows.
The below code shows implementation of class A
classdef A < handle
properties
b = rand
end
end
The below script shows how I would ideally like the operation of finding the mean to look like
a(2,5) = A
mean(a.b, 1);
This does not work however, and the only way I have found to make this work so far is shown below:
a(2,5) = A
mean = [a(1,:).b] + [a(2,:).b] / 2;
This does not look pretty and of course, if one want's to generalize to the n-by-m case, you need a loop to make this work.
Is there a way to achieve this without using a loop, and instead do something like the first example?

 채택된 답변

Rik
Rik 2018년 4월 21일

0 개 추천

If each A.b contains one value, you could use arrayfun to generate a matrix and then do the operation. It's not elegant, but at least it provides a standardized way to do some operations.

mean_val = mean(arrayfun(@(a) a.b,A));

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

질문:

2018년 4월 20일

답변:

Rik
2018년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by