フィルターのクリア

Extracting every n number of column from matrix matlab

34 ビュー (過去 30 日間)
Aybars
Aybars 2018 年 5 月 3 日
コメント済み: Ameer Hamza 2018 年 8 月 16 日
0 down vote favorite I need to extract every n number of column from my matrix. Please note that not every nth column. For example I have a matrix:
A =
1 23 34 53 67 45 67 45
12 34 45 56 67 87 98 12
1 2 3 45 56 76 87 56
And I want to extract each 3 column, so my data should be like:
X =
1 23 34 67 45 67
12 34 45 67 87 98
1 2 3 56 76 87
So I would skip 4th column then 8th column and so on. I know how to extract every nth column and row but I couldn't figure it out what I need. Appreciated for your help

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 3 日
編集済み: Ameer Hamza 2018 年 5 月 3 日
Here is a general solution to your problem in which you can specify how many columns to keep and how many to delete
colToKeep = 2;
colToDelete = 3;
ColIndex = mod( 0:size(A,2)-1, colToKeep+colToDelete ) < colToKeep;
A(:, ~ColIndex) = [];
Use colToKeep and colToDelete to specify the values.
  6 件のコメント
Ee Woon Lim
Ee Woon Lim 2018 年 8 月 16 日
How do we do this for rows?
Ameer Hamza
Ameer Hamza 2018 年 8 月 16 日
Just take the transpose of the matrix and apply the same method. At the end take the transpose again to get the correct matrix.

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

その他の回答 (2 件)

Yuvaraj Venkataswamy
Yuvaraj Venkataswamy 2018 年 5 月 3 日
編集済み: Yuvaraj Venkataswamy 2018 年 5 月 3 日
Answer is below where A is a your matrix.
if true
n=4;
A =[1 23 34 53 67 45 67 45
12 34 45 56 67 87 98 12
1 2 3 45 56 76 87 56]
A(:,n:n:end) =[]
end
  1 件のコメント
Aybars
Aybars 2018 年 5 月 3 日
Hi, Thank you very much for your answer. It is correct. But in that case, how can I adjust the number of column that I want to skip ? For example I would like to take first 2 columns, then skip 3 columns then take 2 columns again.

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


Guillaume
Guillaume 2018 年 5 月 3 日
X = A;
X(:, 4:4:end) = [];
  1 件のコメント
Aybars
Aybars 2018 年 5 月 3 日
Hi, Thank you very much for your answer. It is correct. But in that case, how can I adjust the number of column that I want to skip ? For example I would like to take first 2 columns, then skip 3 columns then take 2 columns again.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by