Is there any way to get this code executed ? Or is there some command for my problem?

3 views (last 30 days)
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
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.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 16 Jan 2016
John - look closely at the condition in your if statement
if X(j,:) == 0
Remember that X(j,:) is a 1x5 array, so when you compare it with zero, you are comparing each element of this arrays with zero. That means that a simple true or false is not returned, but rather an array of logical (true or false) elements. For example,
>> X(1,:)
ans =
0 0.0000 -0.1054 -0.0488 0
and so
>> X(1,:) == 0
ans =
1 0 0 0 1
So what you are really interested in here is whether is how many zeros there are (because that will impact your mean). So you may want to do something like this instead
for j=length(Z-1):-1:1
numZeros = sum(X(j,:) == 0);
% do the condition
end
Now you have to determine when the zeros should be summed into the mean. You say that only the first row has valid zeros in it. If that will always be true, then your code becomes
for j=length(Z-1):-1:1
numZeros = sum(X(j,:) == 0);
if j==1 % first row so include the zeros
else % not the first row so don't include the zeros
end
end
Though it isn't clear to me why you use the length of Z to calculate the probabilities in P and not iterate over the rows of X.
One problem with the above is that you are assuming that the only valid zeros in your data are in the first row of X. Will this always be true? Perhaps try replacing all of the invalid zeros with inf instead. Then when iterating over each row of X you could use isinf to determine which elements of the row are inf. For example,
data = [inf 1 2 3 4 5 inf 6 7 8 9 10 inf]; % data with three inf elements
infData = isinf(data); % an array of ones and zeros
data(infData) = []; % an array with the inf elements removed
Now your row array data has all of its inf values removed, and you can compute the mean.
As for creating an accumulator, please provide more context for how it is used. I see you using an i but it doesn't accumulate. A short example may be needed.
As an aside, it is good practice to use indexing variables other than i and j since MATLAB uses both to represent the imaginary number.

More Answers (2)

Jan
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
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.

Community Treasure Hunt

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

Start Hunting!