フィルターのクリア

How can I select particular elements by an increasing step in rows?

3 ビュー (過去 30 日間)
ugur uresin
ugur uresin 2018 年 4 月 18 日
コメント済み: ugur uresin 2018 年 4 月 18 日
10 20 30
11 21 31
12 22 32
13 23 33
14 24 34
15 25 35
16 26 36
17 27 37
18 28 38
19 29 39
For example: I'd like to extract 4 elements in each column from the matrix above. Extracted elements would be (1,1),(2,1),(3,1),(4,1) for the 1st column, and (1,2),(3,2),(5,1),(7,1) for the 2nd column, and (1,3),(4,3),(7,3),(10,3). Example out is given below:
10 20 30
11 22 33
12 24 36
13 26 39
Note: I have hundreds of rows and columns. So, I need a generic solution.
Thank you.
  1 件のコメント
ugur uresin
ugur uresin 2018 年 4 月 18 日
By the way the number of selected element (e.g. 4) is not a constant!

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

採用された回答

Stephen23
Stephen23 2018 年 4 月 18 日
編集済み: Stephen23 2018 年 4 月 18 日

This is easy using linear indexing (a loop is not required):

>> [M(1:10:end);M(2:11:end);M(3:12:end);M(4:13:end)]
ans =
   10   20   30
   11   22   33
   12   24   36
   13   26   39

Note that M has 10 rows, so you can easily adjust the code I gave to work for any number of rows:

>> S = size(M,1);
>> [M(1:S+0:end);M(2:S+1:end);M(3:S+2:end);M(4:S+3:end)]
ans =
   10   20   30
   11   22   33
   12   24   36
   13   26   39

Note that this method only works if the number of columns is <= the number of rows.

To adjust for N elements from each column you can add a loop, either following the method above:

N = 4;
Y = nan(N,size(M,2));
S = size(M,1);
for k = 1:N
    Y(k,:) = M(k:S+k-1:end);
end

or doing a naive implementation of what you explained in your question.

  2 件のコメント
ugur uresin
ugur uresin 2018 年 4 月 18 日
Dear Stephen,
It might be missed but I need a more ' generic solution' like a for loop since there are hundreds rows and columns in my data.
The matrix that I gave above is just an example.
Sincerely,
ugur uresin
ugur uresin 2018 年 4 月 18 日
Your edited answer works. Thanks in advance!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by