Trouble calculating column mean from a matrix - using different indices for each row

1 回表示 (過去 30 日間)
David McVea
David McVea 2019 年 10 月 16 日
コメント済み: Matt J 2019 年 10 月 17 日
Hello,
I am trying to calculate a column of means from a 2-D matrix. The length of the mean is equally to the number of rows of the matrix.
However, the index to each row is different. For example, row 1 I want the mean of colums 4 to 8. Row 2 I want the means of columns 3 to 5.
I have tried
mean(myMatrix(:,VariableIndex1:VaribleIndex2))
Where VariableIndex1 and VariableIndex2 are columns for the start and end indices of the columns I need means from
For the example above this would be:
mean(myMatrix(:,[4;3]:[3:5]))
- but this seems to give a non-sensical result.
Any thoughts or advice on how to do this? The column acutally has 100 000 rows so I don't want to loop through it.
Thanks.

採用された回答

Matt J
Matt J 2019 年 10 月 16 日
編集済み: Matt J 2019 年 10 月 17 日
[m,n]=size(myMatrix); %Edited
e=1:n;
mask=VariableIndex1(:)<=e & e<=VariableIndex2(:);
result=sum(myMatrix.*mask,2)./sum(mask,2);
  2 件のコメント
David McVea
David McVea 2019 年 10 月 17 日
Hi - perfect, thanks. Except note the first line should be
[m,n]=size(myMatrix);
Matt J
Matt J 2019 年 10 月 17 日
Right you are!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by