フィルターのクリア

IF loop to select values superior of a threshold in a matrix

1 回表示 (過去 30 日間)
Louise
Louise 2020 年 12 月 11 日
コメント済み: Louise 2020 年 12 月 11 日
Hi all,
For the matrix A (see attached), my goal is to calculate the mean of each row in the fifth column with the following conditions :
If for the column 1 to 4, values are superior to 0,84 , calculate the mean of the four columns.
If for the column 1 to 4, values are not superior to 0,84 (for instance if column 3 = 0,76), calculate only the mean of the first, second and four columns.
I reach the loop for the first case but I'm not able to do that for the second case....
Here are the few lines I've started to write :
for i=1:100
if A(i,1:4)>0.85
A(i,5)=mean(A(i,1:4));
else if (I have a doubt for this condition)
A(i,1) or A(i,2) or A(i,3) or A(i,4) < 0,85 (I have a doubt for this line)
................................................................
end
end
Thanks in advance for your help,
Louise

採用された回答

Jan
Jan 2020 年 12 月 11 日
編集済み: Jan 2020 年 12 月 11 日
for k = 1:100
match = (A(k, 1:4) > 0.85);
A(k, 5) = sum(A(k, match)) / sum(match);
end
This works faster without a loop:
match = (A(:, 1:4) > 0.85);
A(:, 5) = sum(A(:, 1:4) .* match, 2) ./ sum(match); % Auto-expand, >= Matlab R2016b
Note that "if A(i,1:4)>0.85" might not do, what you expect. The condition of a if statement must be a scalar. So Matlab inserts an all() command implicitely.
  1 件のコメント
Louise
Louise 2020 年 12 月 11 日
Thanks for the tip without a loop !
I've just slightly modify your code as below to get the mean for each row.
match = (A(:, 1:4) > 0.85);
for i=1:100
A(i, 5) = sum(A(i, 1:4) .* match(i,1:4)) ./ sum(match(i,:));
end
Have a nice day

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by