For loop for applying filter for each column

1 次查看(过去 30 天)
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

采纳的回答

Greig
Greig 2015-2-17
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 个评论
Bharath
Bharath 2015-2-19
编辑:Bharath 2015-2-19
Thanks a ton. It was very informative and it works perfectly..! :) So is it always good to preallocate before start of a loop? In my previous case I just defined as
ii= 1:28
It just read only the last column but now it seems working perfectly good with the preallocation.
Greig
Greig 2015-2-19
Your loop read through all of the columns, but since Xnew was overwritten each time, it only remembered the last loop.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by