How to group elements in arrays?

Let's say I have a 1 x 5 structure array, 'data' with fields 'f1' 'f2' 'f3' 'f4' 'f5'. What I am trying to do is to group the elements of the array in f1 three by three, and find the corresponding mean of the elements in f2 - f5. Can anyone help me to do this? Thanks!

2 件のコメント

Murali Krishna
Murali Krishna 2015 年 5 月 28 日
can you clarify what do u mean by "group the elements of the array in f1 three by three"
bio lim
bio lim 2015 年 5 月 28 日
There are five arrays in each field. (all of them are 1x20). Let's say the first array in the f1 field looks like this. data(1).f1 = [data(1).f1(1) data(1).f1(2)........ data(1).f1(20).
I want to group the elements three by three like (data(1).f1(1), data(1).f1(2), data(1).f1(3)). By doing this, I wanted to find the corresponding mean of the other fields.
mean(data(1).f2(1), data(1).f2(2), data(1).f2(3)), and so on. The problem is I have a large number of arrays, and wanted to use a loop. Any idea? Thanks.

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

 採用された回答

Murali Krishna
Murali Krishna 2015 年 5 月 28 日
編集済み: Murali Krishna 2015 年 5 月 28 日

0 投票

I think this may solve ur problem
m=floor(length(data(1).f1)/3)
for l=1:1:length(data)
t=0;
for k=1:1:m
s1(l).a(k)=mean(data(l).f1(1+t:3+t));
s1(l).b(k)=mean(data(l).f2(1+t:3+t));
t=t+3;
end
end
I assume that the length of the fields f1 to f5 is same. check for structure s1.a,s1.b...for the mean of 3 elements of respective fields..

1 件のコメント

bio lim
bio lim 2015 年 6 月 2 日
Are there any ways if the lengths of the fields are not same? Thanks!

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

その他の回答 (0 件)

カテゴリ

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

製品

質問済み:

2015 年 5 月 28 日

コメント済み:

2015 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by