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!

 Réponse acceptée

Thanks for the help! My problem was sort of difficult and while I didn't use your suggestion in that explicit manner it did point me in the right direction. I ended up being able to figure this out with a combination of indexing and looping as you can see below. The difference between the netcfq array I referred to in the original question and the cfq variable referenced below is that row 1 in cfq is the initial cash outlay (a negative number) while netcfq are the cash inflows. The IRR calculation won't work with zeros or extremely small numbers as I found out by setting netcfq(netcfq<0) = 1/10^9 and then using the IRR function.
Thanks again!
Code:
warning('off','finance:irr:multipleIRR')
%IRR Calculations
irr_index_nz = sum(netcfq>0)+1; % row subscript of last day of non-zero cash flow
irr_value = zeros(1,size(cfq,2));
for i = 1:size(cfq,2)
rows = 1:irr_index_nz(i);
column = (i);
irr_value(i) = irr(cfq(rows, column))*12;
end

Plus de réponses (1)

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.

3 commentaires

I can't seem to get the for loop working. The method for summing worked fine, but I can't get it to work for the matlab irr function which cannot handle zeros and which treats the first element of the input array (a negative number) as a cash inflow and the rest of the positive non-zero elements of the array as cash inflows.
Creating the temporary array you are talking about also reduces my initial array to a single column of elements and doesn't maintain the column structure, which I need. Is there an easy way to run irr(netcfq) for just non-zero elements by column?
Perhaps I'm using the for-loop wrong but I don't think so.
try this:
myres = zeros(1,size(netcfq,2));
for i = 1:size(netcfq,2)
myres(i) = irr(netcfq(netcfq(:,i)>0));
end
Thanks again for the help, please see my answer below.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by