Mean of array columns with condition

I have an array of which I need the mean of each column (X). Additionally, I have a logical array of which rows meet a condition and should be included in the mean (Y).
Naturally, the mean function in MATLAB calculates each column mean, but when I insert the logical vector for the row selection, the mean is calculated for the whole array. How do I fix this for computing the mean of each column in X for the valid indices of Y?
When entering
means_active = mean(X(Y), :);
I get the error 'Reference to a cleared variable mean.', though I don't want to use any variable 'mean' but the built-in function 'mean'.

3 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 18 日
Please illustrate with example, rather than texts?
Mariella Dreißig
Mariella Dreißig 2019 年 11 月 18 日
編集済み: Mariella Dreißig 2019 年 11 月 18 日
I already copied the line of code I'm talking about, I don't know how to illustrate more.
means_active = mean(X(Y(:,1) < cutoff), :);
Mariella Dreißig
Mariella Dreißig 2019 年 11 月 18 日
I found a workaround though, thanks :)

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

回答 (1 件)

Philippe Lebel
Philippe Lebel 2019 年 11 月 18 日

0 投票

I tested my solution with these matrices:
a = rand(6,3)
B=[1;0;1;1;0;1];
means_active = mean(a(logical(B)),2);
seems to work for me. (i was forced to cast B as logical because i manually declared the values inside the list as integers)

カテゴリ

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

製品

タグ

質問済み:

2019 年 11 月 18 日

回答済み:

2019 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by