How to loop through a dataset and skip every other row?

I have a dataset and would like to create a loop to skip every other row and get the data pertaining to that data. for example:
  • A 1,2,3,4,5,6
  • B 7,8,9,10,11,12
  • C 13,14,15,16,17,18
I would like to write a for loop to get the data in rows A,C, E, G, etc.

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 13 日

0 投票

If M is your matrix
out=M(1:2:end,:)

10 件のコメント

andrew
andrew 2014 年 1 月 13 日
is there a way to choose a specific row to start for example lets say I wanted to start from row B and have it print B,D,F, etc
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 13 日
out=M(2:2:end,:)
andrew
andrew 2014 年 1 月 14 日
the data contains subsets for example a1, a2, a3, a4, a5...a24, b1, b2, b3....b24, etc
how can I just print the following rows: a2, a4, a6, a8....o24 b1,b3,b5,b7,b9....p23 b2,b4,b6,b8....p24
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 14 日
If you mean, for example
data=[1 2 3 4 5 6 7 8 9 10]
out=data(2:2:end)
It's just like I said above
andrew
andrew 2014 年 1 月 14 日
編集済み: andrew 2014 年 1 月 14 日
Maybe this will clarify my question:
The code that you gave me works and I just need to be able to get rid of the ones that ends in for example A(3,5,7,9,11,13,15,17,19,21,23...
outcome should be (1,A2)(1,C2)(1,E2)(1,G2)(1,I2)(1,K2)(1,M2)(1,O2), (1,A4)...etc...
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 14 日
There is no, in Matlab, data like
(1,A2)(1,C2)(1,E2)(1,G2)(1,I2)(1,K2)(1,M2)(1,O2), (1,A4).
What class of data are using?
andrew
andrew 2014 年 1 月 14 日
these were imported and represent a location.
José-Luis
José-Luis 2014 年 1 月 14 日
Those are strings. Are they stored in a cell array?
andrew
andrew 2014 年 1 月 14 日
they are stored in data9.Location
José-Luis
José-Luis 2014 年 1 月 14 日
That's the variable name. What's the variable type? Most likely a cell array. You can find out by:
class(data9.Location)

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

José-Luis
José-Luis 2014 年 1 月 14 日
編集済み: José-Luis 2014 年 1 月 14 日

0 投票

If you indeed have a cell array of strings, this should work:
your_data = cellfun(@(x) x(3:end),data9.Location,'uniformoutput',false)
Please accept an answer if it helps you.

2 件のコメント

andrew
andrew 2014 年 1 月 14 日
is there a way to sort the column so that all the A's, are together?
José-Luis
José-Luis 2014 年 1 月 14 日
That's a different question.
sort(your_data)
Please accept an answer if it helps you.

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

カテゴリ

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

タグ

質問済み:

2014 年 1 月 13 日

コメント済み:

2014 年 1 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by