For loop for applying filter for each column

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 日

1 投票

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 月 17 日
編集済み: Bharath 2015 年 2 月 18 日
Thanks for your answer. I still have the peaks at the 0 frequency when I use the 'detrend' for the complete signal. Its getting reduced when I apply the filter for individual columns. I don't know why. Looks quiet strange. Any possible alternative ways of doing it?
Bharath
Bharath 2015 年 2 月 18 日
I get this error when I even Xnew is indexed ??? Attempted to access X(:,2); index out of bounds because size(X)=[4096,1].
Ilham Hardy
Ilham Hardy 2015 年 2 月 18 日
The answer is on the error message, you want to put data into second column of X, hence X(:,2) , which does not exist since the size of X is 4096x1 (array, not matrix/table).
Bharath
Bharath 2015 年 2 月 18 日
Thanks for your answer. But my initial matrix was of size 4096x28. And then I defined for loop. But Now I guess my for loop is overwriting the 'X' matrix. How to avoid that. I mean read every column of X and apply the filters and store them in Xnew columnwise.
Greig
Greig 2015 年 2 月 19 日
If you really insist on using a loop try this....
X=rand(4096, 28); % My test data
Xnew = NaN(size(X)); % Preallocate for speed
for ii=1:size(X, 2)
Xnew(:,ii) = X(:,ii) - mean(X(:,ii));
end
[It is recommended not to use "i" or "j" as an indices given that they are sqrt(-1).]
Then compare with these...
Xnew2 = X - repmat(mean(X), size(X, 1), 1);
Xnew3 = detrend(X,0);
Of course if you want to apply more complicated filters than mean subtraction, the loop version may be the better option.
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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2015 年 2 月 17 日

コメント済み:

2015 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by