Borrar filtros
Borrar filtros

how to specify numbers in an array with varying steps

38 visualizaciones (últimos 30 días)
A Poyser
A Poyser el 26 de Jun. de 2024 a las 10:12
Respondida: Star Strider el 26 de Jun. de 2024 a las 10:22
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 comentario
Aditya
Aditya el 26 de Jun. de 2024 a las 10:19
For N2 you can create it like this: N2 = [2:1:4, 10:1:11]

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 26 de Jun. de 2024 a las 10:22
For ‘N2’ and others, just use the individual aray indices in a vector —
N = rand(1,14)
N = 1x14
0.9276 0.2641 0.4683 0.1720 0.9337 0.3857 0.6798 0.7333 0.7803 0.1052 0.2883 0.7299 0.7880 0.4599
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N2 = N([2:4 10:11])
N2 = 1x5
0.2641 0.4683 0.1720 0.1052 0.2883
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N2 = N([2,3,4,10,11])
N2 = 1x5
0.2641 0.4683 0.1720 0.1052 0.2883
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note the use of square brackets [] here to concatenate the individual elements.
.

Más respuestas (1)

Namnendra
Namnendra el 26 de Jun. de 2024 a las 10:20
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.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by