フィルターのクリア

Replace elements in a SxS matrix with zero, in each column but in different rows

1 回表示 (過去 30 日間)
Hi people,
I know how to simply replace elements with zero, but I'm stuck with this problem. I have a matrix 2108x62 and I need to replace elements in column #1 and row 1-34 by zero, then in column #2 but row 35-68, then column #3 but row 69-102 etc to the end of column 62 and row 2075-2108.
Best,
Miroslav

採用された回答

Stephen23
Stephen23 2017 年 2 月 1 日
編集済み: Stephen23 2017 年 2 月 1 日
This is simple using either the FEX submission expand:
>> mat = randi(9,2108,62);
>> idx = expand(eye(62)==1,[2108/62,1]);
>> mat(idx) = 0;
or for newer MATLAB versions repelem:
>> idx = repelem(eye(62)==1,2108/62,1);
  1 件のコメント
Miroslav Josic
Miroslav Josic 2017 年 2 月 2 日
Hi Stephen,
Thanks a lot for your quick reply. It works perfectly using repelem function.
I really appreciate your help on this!
Best,
Miroslav

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

その他の回答 (1 件)

David J. Mack
David J. Mack 2017 年 2 月 1 日
編集済み: David J. Mack 2017 年 2 月 1 日
Hey Miroslav,
have you looked at the sub2ind function? Should be something like the following.
% Let M be your 2108*64 matrix.
iRow=(1:2108)'; %Row indicesin i-j notation
jCol=floor(iRow/34)+1; %Column indices using DIV in i-j notation
ind=sub2ind([2108 62],iRow,jCol); %Linear indices (e.g. 1,1 = 1; n,m=n*m)
M(ind)=0;
Greetings, David
  1 件のコメント
Miroslav Josic
Miroslav Josic 2017 年 2 月 2 日
Hi David,
Thanks for your quick help.
Unfortunately, it doesn't help much, it says "Out of range subscript". Probably because i get in j column, 1 for rows 1-33 (so the window is 33 actually), then the window is 34 for the rest of the rows. In the end, I miss one row in the sample, therefore the value for last row 2108 is 63.
Miroslav

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by