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
2014년 1월 13일
If M is your matrix
out=M(1:2:end,:)
댓글 수: 10
andrew
2014년 1월 13일
Azzi Abdelmalek
2014년 1월 13일
out=M(2:2:end,:)
andrew
2014년 1월 14일
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
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
2014년 1월 14일
José-Luis
2014년 1월 14일
Those are strings. Are they stored in a cell array?
andrew
2014년 1월 14일
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)
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.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
