For loop for applying filter for each column

8 views (last 30 days)
Bharath
Bharath on 17 Feb 2015
Commented: Greig on 19 Feb 2015
Hi I've a signal which I read into matlab as a 4096x28 matrix. I want to apply for loop as follows which will read one column apply the filter and then store the values and then moves to the next column.
X = reshape(T_hor,[],28); % forming 4096x28 matrix
for i = 1:28;
Xnew = X(:,i) - mean(X(:,i)); % filter for DC offset
end;
It just reads the last column (28th) and stops. Is something wrong with my code? Can someone help me to fix this. Thanks in advance.
thanks in advance

Accepted Answer

Greig
Greig on 17 Feb 2015
Xnew is not indexed. You should have Xnew(:, i).
You could do this without the loop. One such way would be...
Xnew = X - repmat(mean(X), 4096, 1);
Another is....
Xnew = detrend(X,0);
I would go with the second (less typing), but either will save you some time in a loop.
  7 Comments
Greig
Greig on 19 Feb 2015
Your loop read through all of the columns, but since Xnew was overwritten each time, it only remembered the last loop.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!