Is there any way to get this code executed ? Or is there some command for my problem?
Show older comments
Initially, my data had a different number of rows for each individual column. Having said that, I was able to add "0" for all the data that so that they all had a row equal to the max number of rows. Now, Im in a situation where I want to find the mean of each column but without accounting for the 0's that I added. Thus, I need to tell matlab to stop if it sees a zero but the problem is that some of the data actually has "0"'s in 1 first row for example. So far what I have is something along the lines of this by executing an for-loop combined with an if statement as follows:
d = 0.95;
Z = [0 2 3; 4 5 6; 7 8 9; 0 9 0; 0 0 0; 0 0 0];
i = 1;
for j = length(Z-1):-1:1
if X(j,:) == 0
P(j,:) = (1/i)*sum(((1-d)*(d.^(i)))*Z);
else
P(j,:) = (1/(i + 1))*sum(((1-d)*(d.^(i)))*Z);
end
end
- So for the matrix that I have constructed, how can I tell it to count the zero with the first row but not the last 2 rows ? So far my if statement does not work at all and it just outputs a value in the first row and then the same values in the rest.
- Also, I am wondering how I can at the same time build up an accumulator that counts the number of rows it has already gone through. I am not sure if I have done it properly in my for loop here but the idea is that I want to add it to the number of samples for the calculation of my mean if it is not 0 and increase it for each non-zero value. I am not sure if the i will continue to accumulate in each loop in this case if I predefine it.The j is the fact that I want to do a reverse starting from the bottom so that the most recent date has the most weight placed to it. Any help is much appreciated in advance. I have attached a data file to work with if my explanation is not clear.
1 Comment
Jan
on 16 Jan 2016
The posted code is strange. Do you really mean length(Z-1) which is the same as length(Z)? What is the meaning of calculating 1/i and d.^i , when i is set to 1? I do not see the relation between your description and the code. What is X? The condition X(j,:) == 0 is a vector, such that an all() is added internally, but it is not clear, if this is wanted.
Why do you pad the matrix with zeros, when the values contain zeros also? Then you cannot recognize if the data contain a trailing zero anymore. So padding with NaN would be smarter.
Accepted Answer
More Answers (2)
Jan
on 16 Jan 2016
Based on some sentences in the text of the message: Obtain the mean of each column ignoring the trailing zeros:
Z = [0 2 3; 4 5 6; 7 8 9; 0 9 0; 0 0 0; 0 0 0];
Index = bsxfun(@times, Z~=0, (1:size(Z, 1)).');
Len = max(Index, [], 1);
Result = sum(Z, 1) ./ Len;
If you really want the rows the modifications are easy.
Walter Roberson
on 17 Jan 2016
num_leading_vals = sum( cumprod(Z ~= 0, 2), 2);
The above is per row. If you want per column then change the 2 to 1.
1 Comment
Putsandcalls
on 17 Jan 2016
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!