Sum Object Properties: Signal

조회 수: 4 (최근 30일)
Nycholas Maia
Nycholas Maia 2018년 1월 11일
댓글: Nycholas Maia 2018년 1월 12일
I create my object using this class:
classdef myClass
properties
signal = [1 1 2 2 3 3];
end
end
The I instantiate N object of this class:
N = 1000;
for i = 1:N
obj(N) = myClass;
end
I would like to sum the 'signal property' of all N object and return a vector with same size of 'signal property' (6 col x 1 row).
Like this:
result_vector = Sum(obj.signal);
The result should be like:
[X X Y Y Z Z]
Is there any optimized way to do this sum operation without using a FOR loop? How can I do that?

채택된 답변

Gabriele Bunkheila
Gabriele Bunkheila 2018년 1월 12일
I agree with the previous suggestion, another close variation being:
sum(vertcat(obj.signal))
If myClass was instead a System object (such as many filters and other signal processing algorithms found in MATLAB toolboxes), then you'd want to use cell arrays instead, via something like the following:
N = 1000;
obj = cell(N,1);
for i = 1:N
obj{i} = myClass;
end
In that case, I believe the syntax could get a bit more involved so I thought I'd include an example for reference:
sum(cell2mat(cellfun(@(c) c.signal, obj,'UniformOutput',false)))
Thanks,
Gabriele.
  댓글 수: 1
Nycholas Maia
Nycholas Maia 2018년 1월 12일
Brilliant solution Gabriele!
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by