how to specify numbers in an array with varying steps
15 views (last 30 days)
Show older comments
I want to specify the a group of numbers in an array but they do not appear in even steps.
I want to capture
N1 (1 and 12), N2 (2,3,4,10,11), N3(5,6,7,8,9) and N4 (13, 14)
I understand that to capture N1,and N4 I can specify the individual array numbers, and for N3 I can specify the start and the end and the size of the steps [5:1:9]. I just can't work out how to specify what I would like in the instance of N2
Please help.
Thanks
Alex
1 Comment
Accepted Answer
Star Strider
on 26 Jun 2024
For ‘N2’ and others, just use the individual aray indices in a vector —
N = rand(1,14)
N2 = N([2:4 10:11])
N2 = N([2,3,4,10,11])
Note the use of square brackets [] here to concatenate the individual elements.
.
0 Comments
More Answers (1)
Namnendra
on 26 Jun 2024
Hello Alex,
To specify a group of numbers in an array that do not appear in even steps, you can manually create the array by specifying each element individually. For `N2`, which includes the numbers `[2, 3, 4, 10, 11]`, you can simply list these numbers in an array.
Here's how you can define all the groups in MATLAB:
% Define the groups
N1 = [1, 12];
N2 = [2, 3, 4, 10, 11];
N3 = [5:1:9]; % or simply [5, 6, 7, 8, 9]
N4 = [13, 14];
% Display the groups
disp('N1:');
disp(N1);
disp('N2:');
disp(N2);
disp('N3:');
disp(N3);
disp('N4:');
disp(N4);
In the case of `N2`, you need to manually list each number because they do not follow a consistent step pattern. The other groups (`N1`, `N3`, and `N4`) can be defined using ranges or individual elements as appropriate.
Explanation:
- N1: `[1, 12]` - This group contains the numbers 1 and 12.
- N2: `[2, 3, 4, 10, 11]` - This group contains the numbers 2, 3, 4, 10, and 11. Since these numbers do not follow a consistent step pattern, you list them individually.
- N3: `[5:1:9]` - This group contains the numbers from 5 to 9 with a step size of 1. You can also specify it as `[5, 6, 7, 8, 9]`.
- N4: `[13, 14]` - This group contains the numbers 13 and 14.
By defining the groups in this way, you can easily capture and work with the specified numbers in your MATLAB code.
I hope the above information helps you.
Thank you.
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps 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!