Grouping elements in an array?

2 ビュー (過去 30 日間)
bio lim
bio lim 2015 年 6 月 2 日
コメント済み: Azzi Abdelmalek 2015 年 6 月 2 日
Hello. I have a 1 x 46 struct array 'data' with two fields, 'Altitude' and 'Velocity'. Each array has different sizes but the corresponding two arrays for the two fields have same sizes. For example, when I type in:
counts = array(@(x) numel(x.Altitude), data)
counts =
Columns 1 through 19
100 335 104 74 294 101 0 ... until the column goes 46.
What I am trying to do is to group the elements of each arrays in 'Altitude' seven by seven and find the mean.Then, find the mean of the corresponding the elements in the 'Velocity' field. Then I would like to compare the each mean of the 'Altitude' arrays, and plot it.
Could anyone help me with this? Any help is greatly appreciated as I am clueless here. Thank you. The code I have written so far is:
for n = 1 : length(data)
interval_number = floor(length(data(n).Altitude)/7);
t = 0;
for k = 1 : interval_number
s1(n).a(k) = mean(data(n).Altitude(1+t:7+t));
s1(n).b(k) = mean((data(n).Velocity(1+t:7+t)));
t = t + 7;
end
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 6 月 2 日
編集済み: Andrei Bobrov 2015 年 6 月 2 日
x = struct2cell(data);
out = cellfun(@(y)nanmean(reshape([y,nan(1,mod(-numel(y,7))],7,...
[])),squeeze(x),'un',0);

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 2 日
編集済み: Azzi Abdelmalek 2015 年 6 月 2 日
v=struct('Altitude',num2cell(1:46),'Velocity',num2cell(randi(9,1,46)))
ii=0;
m=numel(v)
for k=1:7:46
ii=ii+1
k1=k
k2=min(m,k+6)
out(ii).Altitude=mean([v(k1:k2).Altitude])
out(ii).Velocity=mean([v(k1:k2).Velocity])
end
  2 件のコメント
bio lim
bio lim 2015 年 6 月 2 日
編集済み: bio lim 2015 年 6 月 2 日
I am so sorry but I am a beginner in MATLAB. Could you help by explaining briefly your code, and tell if my code is still viable? Could you also explain why k = 1 : 7 : 10? Thanks!
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 2 日
No, it's for k=1:7:46

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by