How do you operate on only the non-zero elements of columns within an array?
古いコメントを表示
Problem: I have an m-by-n matrix of values, some positive and some negative, and I am only concerned with values greater than zero. I used logical operators to set all values in the original array that are less than zero to zero, and would like to then be able to do some operation on only those column values that are greater than zero, for instance, find the IRR of the column for all values >0.
I have found the row subscripts (and linear indices) for the first element and last non-zero element in each of the columns, but can't figure out how to use these to calculate the IRR/SUM/other operation on each column individually.
Attached is the variable I am referencing. I am trying to apply a function (IRR) to only positive rows and return the IRR for the positive elements in each column (so rows 1:317 for column 1).
Thanks in advance for your help!
採用された回答
その他の回答 (1 件)
Sara
2014 年 7 月 7 日
For sum:
netcfq(netcfq<0) = 0;
mysum = sum(netcfq,1)
for other functions, you could use a loop on the columns and a temporary array temp = netcfq(netcfq>0) as input.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!