필터 지우기
필터 지우기

Multiplying values in one column with values in another, depending on criteria

조회 수: 1 (최근 30일)
Hi Everyone,
Suppose I have the following matrix:
A =
1.0__0.7__4.4__0.9__50.9
3.0__0.8__5.2__1.0__28.7
6.0__0.5__3.2__0.7__10.5
7.0__-0.5__3.6__0.8__87.7
Now I wish to calculate a value, which is found as follows:
1. Multiply the values in column 2, with the corresponding values in column 5, where the corresponding value in column 4 is greater than or equal to 0.8.
2. Sum the values found in step 1.
So, the answer I am looking for is:
0.7*50.9 + 0.8*28.7 + -0.5*87.7 = 14.74
What expression might do this?
Regards,
Ulrik

채택된 답변

Teja Muppirala
Teja Muppirala 2011년 5월 17일
One way to do it:
sum( A(:,2) .* A(:,5) .* (A(:,4) >= 0.8) )
  댓글 수: 2
Teja Muppirala
Teja Muppirala 2011년 5월 17일
Another possibility,
idx = A(:,4) >= 0.8;
sum( A(idx,2) .* A(idx,5) )
Ulrik Nash
Ulrik Nash 2011년 5월 17일
Thank you very much for the swift reply, Teja!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by