how do I grab multiple multiple intervals of a given variable and put them into a new variable?
Show older comments
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)
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:
Star Strider
on 14 Jan 2016
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
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!