The output value of the for loop is wrong

Hi all,
The output structure is right but somehow the logic is wrong
please find the script below
D_Calc=[];
Filter=[];
for jj=1:12
D_Calc(:,end+1)=D(:,jj); % both D_Calc and D are same
Filter=D_Calc(D_Calc(:,jj)<PlusThreesigma(jj))
MeanFilter(:,end+1)=mean(Filter);
end
output
The filter and the mean filter value takes only the 1st column.

4 Comments

We cannot run the code you provided, as D and PlusThreesigma re undefined.
'The filter and the mean filter value takes only the 1st column.' What does this statement mean? Are you saying that Filter and MeanFilter only give results for the first column? That they only use the first column of the input, so all the results are identical? That something with D_Calc is not correct? Please explain more. What kind of result are you seeing, and how would you expect it to be different.
Hi,
Please find my attached data as reference.
if you run the following script you can see the output
D_Calc=[];
Filter=[];
MeanFilter=[];
for jj=1:12 %need to define the length
D_Calc(:,end+1)=D(:,jj);
Filter=D_Calc(D_Calc(:,jj)<PlusThreesigma(jj))
MeanFilter(:,end+1)=mean(Filter);
end
For me only mean filter is important.
In each iteration it should look first col of D_Calc, Filter the values which are less than three sigma value in the 1st col then it should calculate the mean of that Filter column and save in MeanFilter( of 1st col) and for
second iteration it should check the D_Cal of 2nd ,Filter the values which are less than three sigma value in the 2nd col then it should calulate the mean of that col and save in mean filter (now in 2nd col) and so on.
Ok, I was able to run your code, and I am not seeing the problem you are mentioning. For me, MeanFilter is coming out as a single row of values, with corresponding means in each column.
yes exactly that is my problem.
the mean of each col is right. But the Filter value is not right. I am wondering whether my condition in 'Filter=D_Calc(D_Calc(:,jj)<PlusThreesigma(jj))' is checking for each column. In a for loop when the iteration goes increasing 1st, 2nd 3rd col etc.
I think the second iteration is not working somehow..always it calcualtes the first col and in second iteration it also calculates the first col.
As I said i am expecting each temporary col in the filter should be different then it can give different mean for each col.
For example if you check the last column in filter that does not corresponds to the mentioned condition.
Here you can find the last col of Filter
Filter =
0.5442
0.5337
0.5337
0.5630
0.6115
0.5630
0.6211
0.5540
0.6211
0.5540
0.6339
0.5562
As per condtion it should display the values which are less than 0.1154 (this value is from plusthreesigma last col). Therefore the displayed result is not valid

Sign in to comment.

 Accepted Answer

I see it now. Swap the Filter line for the following.
Filter=D_Calc(D_Calc(:,jj)<PlusThreesigma(jj),jj);
Basically, your logic produces the logic for one column, but because a specific column of D_Calc was not specified it was always looking at the first column.

2 Comments

thats is great thanks a lot for your help.
Can you also help with another problem.
Filter=D_Calc(D_Calc(:,jj)>MinusThreesigma(jj),jj);
Is it possible to merge these plus and minus three sigma in one line.
so that the final result shows only data which i need and the rest is excluded.
To use more than one logical condition, use & for and, and | for or.
Filter = D_Calc(D_Calc(:,jj)>MinusThreesigma(jj) & D_Calc(:,jj)<PlusThreesigma(jj),jj)

Sign in to comment.

More Answers (1)

I found the answer if we use the following script it works as i expected
Filter=D_Calc(:,jj);
Filter(Filter>PlusThreesigma(jj))=[];
Filter(Filter<MinusThreesigma(jj))=[];
MeanFilter(jj)=mean(Filter);

Categories

Find more on Loops and Conditional Statements 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!