how do I grab multiple multiple intervals of a given variable and put them into a new variable?

Let's say I have variable A, which is (1:1:100), and I want to create a new variable,B, that is only (30:1:40), (52:1:56),(71:1:75) and (90:1:97), for example. How do I write that in matlab? Thank you!

Answers (2)

You can use indexing to obtain any elements of another array:
A = 1:100
B = A([30:40,52:56,71:75,90:97])
This is covered quite well in the introductory tutorials, which are recommended for all beginners:
If you want to address each separate subset of ‘A’ as elements of ‘B’, put them in a cell array:
A = (1:1:100);
B = { A(30:1:40), A(52:1:56), A(71:1:75), A(90:1:97) };
Example = B{2} % Get Second Subset From ‘B’
Example =
52 53 54 55 56

Tags

Asked:

on 14 Jan 2016

Edited:

on 15 Jan 2016

Community Treasure Hunt

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

Start Hunting!