フィルターのクリア

creating matrix using output elements

2 ビュー (過去 30 日間)
okoth ochola
okoth ochola 2023 年 1 月 19 日
コメント済み: Fangjun Jiang 2023 年 1 月 20 日
Hi, i havecode below which ouputs given values independently. however, i want the ouputs to be under one matrix,what can i add to the code to do this job. B is an n by 1 matrix say B=[1:1:24]'. How can I collect all the values of Hourly_mean to form one matrix? kindly assist. Thank you
B=[1:1:576]'
for k=1:1:numel(B)
Hourly_mean=mean(B(k:24:end))
end
[Hourly_mean]

採用された回答

Fangjun Jiang
Fangjun Jiang 2023 年 1 月 19 日
B=[1:1:576]';
mean(reshape(B,24,[]))
ans = 1×24
12.5000 36.5000 60.5000 84.5000 108.5000 132.5000 156.5000 180.5000 204.5000 228.5000 252.5000 276.5000 300.5000 324.5000 348.5000 372.5000 396.5000 420.5000 444.5000 468.5000 492.5000 516.5000 540.5000 564.5000
  3 件のコメント
Steven Lord
Steven Lord 2023 年 1 月 20 日
Let's take a smaller example that demonstrates the technique. Say I want to take the mean of every 6th element of B. We can reshape B into a matrix.
B = 1:24;
C = reshape(B, 6, 4)
C = 6×4
1 7 13 19 2 8 14 20 3 9 15 21 4 10 16 22 5 11 17 23 6 12 18 24
Now take the mean along the 2nd dimension.
D = mean(C, 2)
D = 6×1
10 11 12 13 14 15
Spot check that D is correct by manually computing the mean of the 3rd, 9th, 15th, and 21st element of B. Does that match D(3)?
sum(B(3:6:24))./4
ans = 12
Fangjun Jiang
Fangjun Jiang 2023 年 1 月 20 日
@Steven Lord, good catch! mean(C,2) is more likely the needed outcome than mean(C).

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 1 月 20 日

カテゴリ

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

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by