I am trying to simplify the code from following loop:
for i = 1:10
for j = 11:20
RS_1(:,i) = signal(:,i) - signal(:,j);
end
for j = 21:30
RS_2(:,i) = signal(:,i) - signal(:,j);
end
for j = 31:40
RS_3(:,i) = signal(:,i) - signal(:,j);
end
for j = 41:50
RS_4(:,i) = signal(:,i) - signal(:,j);
end
RS = [RS_1 RS_2 RS_3 RS_4];
end
With this one:
for i = 1:10
for j = 11:50
for k = 1:40
RS(:,k) = signal(:,i) - signal(:,j);
end
end
end
I am wondering why do I get different matrix RS?

 採用された回答

Massimo Zanetti
Massimo Zanetti 2016 年 9 月 28 日

3 投票

It doesn't work because your are using the RS_n matrices in your second code. This is a simplification of the first code:
RS_1(:,1:10) = signal(:,1:10) - signal(:,11:20);
RS_2(:,1:10) = signal(:,1:10) - signal(:,21:30);
RS_3(:,1:10) = signal(:,1:10) - signal(:,31:40);
RS_4(:,1:10) = signal(:,1:10) - signal(:,41:50);
RS = [RS_1 RS_2 RS_3 RS_4];

3 件のコメント

cniv_we
cniv_we 2016 年 9 月 28 日
編集済み: cniv_we 2016 年 9 月 28 日
Hi thanks for the answer. Is it possible to loop this code? I tried this one:
for i = 1:10
RS_1(:,i) = signal(:,i) - signal(:,10+i);
RS_2(:,i) = signal(:,i) - signal(:,20+i);
RS_3(:,i) = signal(:,i) - signal(:,30+i);
RS_4(:,i) = signal(:,i) - signal(:,40+i);
RS = [RS_1 RS_2 RS_3 RS_4];
end
But if possible I would like to loop 10+i, 20+i, ... as well so that I don't need to use RS_1, RS_2, etc. The dimension of matrix RS is 1000 rows x 40 columns.
Massimo Zanetti
Massimo Zanetti 2016 年 9 月 28 日
Ah ok! simple like this:
for i=[11,21,31,41]
RS(:,i-10:i-1) = signal(:,1:10)-signal(:,k:k+9);
end
That's it.
cniv_we
cniv_we 2016 年 9 月 28 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2016 年 9 月 28 日

コメント済み:

2016 年 9 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by