3-D matrix operations

1 回表示 (過去 30 日間)
Asmita Banerjee
Asmita Banerjee 2020 年 3 月 4 日
編集済み: Matt J 2020 年 3 月 4 日
I have a 5X126 matrix that I am splitting into a [5,21,6] matrix using reshape. I want to average the values of neighboring columns in each of these submatrices and store them into a final 3-d matrix that will be [5 20 6]. How can i write a for loop to do this?
I am very new to Matlab so any help would be appreciated. Thanks!
  3 件のコメント
Asmita Banerjee
Asmita Banerjee 2020 年 3 月 4 日
RefBlock = reshape(FinalRef,5,21,m./21); %%breaking down matrix into separate blocks
SamBlock = reshape(FinalSam,5,20,n./20);
RefBloAvg = zeros(5,20,m./21);
for i = 1:20
for j = 1:(m./21)
RefBloAvg(:,i,j) = (FinalRef(:,i,j) + FinalRef(:,i+1,j))./2;
end
end
this is what I wrote to average neighboring values in my new sub-matrix but I get the error "Index in position 3 exceeds array bounds (must not exceed 1)."
Turlough Hughes
Turlough Hughes 2020 年 3 月 4 日
"I want to average the values of neighboring columns" is a little ambiguous, if we consider a smaller example, such as the following 5 by 5 matrix
>> M = randi(10,[5 5])
M =
8 9 2 4 6
1 8 1 5 9
9 6 10 7 4
10 2 4 1 5
10 4 3 9 1
Is this the output you're expecting on each page as follows:
>> result = M(:,1:end-1) + diff(M,1,2)./2
result =
8.5000 5.5000 3.0000 5.0000
4.5000 4.5000 3.0000 7.0000
7.5000 8.0000 8.5000 5.5000
6.0000 3.0000 2.5000 3.0000
7.0000 3.5000 6.0000 5.0000

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

採用された回答

the cyclist
the cyclist 2020 年 3 月 4 日
Assuming that for the average of columns you want the result that Turlough specified, then
RefBloAvg = movmean(RefBlock,[0 1],2);
RefBloAvg(:,end,:) = [];
Note that this does not need to be in a loop of any kind. It works on the entire array at once.
  1 件のコメント
Asmita Banerjee
Asmita Banerjee 2020 年 3 月 4 日
this works! thank you!

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

その他の回答 (2 件)

Turlough Hughes
Turlough Hughes 2020 年 3 月 4 日
Here's the method I was suggesting. The idea is to take the first value and add half the difference (effectively interpolating)
RefBloAvg = RefBlock(:,1:end-1,:) + diff(RefBlock,1,2)./2;

Matt J
Matt J 2020 年 3 月 4 日
編集済み: Matt J 2020 年 3 月 4 日
Another guess,
result=convn(RefBlock,[1,1]/2,'valid');

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by